0

我正在尝试获取每个类别中最近的位置。谁能帮我做到这一点?

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"))
                            )));
4

1 回答 1

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"))
          )));

有关详细信息,请参阅投影运算符分组运算符

于 2013-05-22T16:16:41.340 回答