我需要从道路集合中选择独特的高速公路。

我想我可以为此使用LINQ。

我有(存根代码)

  Dim roads As List(Of Roads) = myRegion.Roads
  Dim highways As New List(Of Highway)

  For Each road In roads
    If road.RoadType = RoadType.Highway Then
      highways.Add(DirectCast(road, Highway))
    End If
  Next ic

  ' Now I think sorting them by .Id and then remove duplicates '
  Dim myComparer As New HighwayByIdComparer
  highways.Sort(myComparer)


也接受C#变体;)

最佳答案

C#:

return myRegion.Roads
    .Where(x => x.RoadType == RoadType.Highway)
    .DistinctBy(x => x.Id);


(其中DistinctBy是Jon Skeet出色的MoreLINQ项目中定义的扩展方法)

关于c# - LINQ关于从集合中选择不同元素的建议,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8505469/

10-09 03:26