.net 如何从每个类别中选择最近距离
我试图在每个类别中找到最近的位置。谁能帮我做这个.net 如何从每个类别中选择最近距离,.net,linq,.net,Linq,我试图在每个类别中找到最近的位置。谁能帮我做这个 var xml = new XElement("Locations", locations.OrderBy(n => n.CategoryID) .ThenBy(n => distance(lat, lon, (double)n.Latitude, (double)n.Longitude)) .Wh
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"))
)));
var xml=new-XElement(“位置”,
locations.OrderBy(n=>n.CategoryID)
.ThenBy(n=>距离(纬度、经度、纬度、经度)
其中(n=>(距离(纬度,经度,(双)纬度,(双)经度)
新XElement(“位置”,
新XAttribute(“CategoryID”,location.CategoryID),
新元素(“类别”,位置。类别),
新XElement(“LocationID”,location.LocationID),
新XElement(“LocationName”,location.LocationName),
新元素(“纬度”,位置,纬度),
新元素(“经度”,位置。经度),
新元素(“距离”,距离(纬度,经度,(双)位置。纬度,(双)位置。经度),
新XElement(“状态”,(location.HasManagedHours?“管理小时”:“打开”)
)));
我还没有测试过它,但我建议分组是一种方法,比如:
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"))
)));
请参阅和以了解更多信息。第一个选择只是选择顶部的一个,第二个选择是创建元素并返回这些元素?你能做两个这样的选择吗,我从未尝试过?第一个是要求布尔值ahhh好,这很有意义。我应该知道你的意思刚刚测试过,需要切换第一个选择再次更新的SelectMany。谢谢。我想知道为什么它在第二次选择时抛出错误。这看起来很棒。