How to get Distinct Values from List(Of T) using Linq

后端 未结 2 357
我寻月下人不归
我寻月下人不归 2020-12-18 04:48

I have a List(Of Hardware) - the List is called HWModels

Class Hardware has the following Properties:

  • ModelName
  • Status
  • C
2条回答
  •  独厮守ぢ
    2020-12-18 05:14

    LINQ to Objects doesn't provide anything to do "distinct by a projection" neatly. You could group by the name and then take the first element in each group, but that's pretty ugly.

    My MoreLINQ provides a DistinctBy method though - in C# you'd use:

    var distinct = HWModels.DistinctBy(x => x.ModelName).ToList();
    

    Presumably the VB would be something like

    Dim distinct = HWModels.DistinctBy(Function(x) x.ModelName).ToList
    

    Apologies for any syntax errors though :(

提交回复
热议问题