LINQ Group By with multiple parameters

后端 未结 2 1739
暗喜
暗喜 2021-02-04 06:28

I have a VB.NET application and want to do Group By on multiple columns.

Class Structure:

Public Class Person
   Public Property Name as         


        
2条回答
  •  轮回少年
    2021-02-04 06:58

    The problem is that only Key properties in anonymous types are used in equality and hashing in VB. (All properties in C# anonymous types are effectively key properties.) So you just need to change your query to:

    Dim oResult = PersonList _
                    .GroupBy(Function(v) New With { Key v.City, Key v.Country}) _
                    .Where(Function(grp) grp.Count > 1).ToList()
    

    See the documentation for anonymous types in VB for more details.

提交回复
热议问题