我正在尝试获得每个类别中最近的位置。任何人都可以帮我这么做吗?
var xml = new XElement("Locations",
locations.OrderBy(n => n.CategoryID)
.ThenBy(n => distance(lat, lon, (double)n.Latitude, (double)n.Longitude))
.Where(n => (distance(lat, lon, (double)n.Latitude, (double)n.Longitude) <= 5))
.Select(location =>
new XElement("Location",
new XAttribute("CategoryID", location.CategoryID),
new XElement("Category", location.Category),
new XElement("LocationID", location.LocationID),
new XElement("LocationName", location.LocationName),
new XElement("Latitude", location.Latitude),
new XElement("Longitude", location.Longitude),
new XElement("Distance", distance(lat, lon, (double)location.Latitude, (double)location.Longitude)),
new XElement("Status", (location.HasManagedHours ? "Managed Hours" : "Open"))
)));
答案 0 :(得分:1)
我没有测试过,但我建议分组是要走的路,比如:
var xml = new XElement("Locations",
locations
.GroupBy(n => n.CategoryID)
.SelectMany(g => g
.OrderBy(n => distance(lat, lon, (double)n.Latitude, (double)n.Longitude))
.Take(1))
.Select(location =>
new XElement("Location",
new XAttribute("CategoryID", location.CategoryID),
new XElement("Category", location.Category),
new XElement("LocationID", location.LocationID),
new XElement("LocationName", location.LocationName),
new XElement("Latitude", location.Latitude),
new XElement("Longitude", location.Longitude),
new XElement("Distance", distance(lat, lon, (double)location.Latitude, (double)location.Longitude)),
new XElement("Status", (location.HasManagedHours ? "Managed Hours" : "Open"))
)));
有关详细信息,请参阅Projection Operators和Grouping Operators。