我有一个VB.NET应用程序,并希望在多个列上执行Group By.
班级结构:
Public Class Person
Public Property Name as String
Public Property City as String
Public Property Country as String
End Class
Dim oResult = PersonList _
.GroupBy(Function(v) New With {v.City, v.Country}) _
.Where(Function(grp) grp.Count > 1).ToList()
Run Code Online (Sandbox Code Playgroud)
我有多个人记录,其中包含相同的城市名称和国家/地区名称.但上面的查询返回零项.如果我只使用一个列城市或国家,那么它的工作正常.
Dim oResult = PersonList _
.GroupBy(Function(v) v.City) _
.Where(Function(grp) grp.Count > 1).ToList()
Run Code Online (Sandbox Code Playgroud)
任何人都指出我错误的Group By LINQ查询与多个参数.
Jon*_*eet 22
问题是只有Key
匿名类型的属性在VB中用于相等和散列.(C#匿名类型中的所有属性都是有效的关键属性.)因此,您只需将查询更改为:
Dim oResult = PersonList _
.GroupBy(Function(v) New With { Key v.City, Key v.Country}) _
.Where(Function(grp) grp.Count > 1).ToList()
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息,请参阅VB中的匿名类型的文档.
Dim q1 = (From p In Repository.Table
Group p By p.CreatedDate.Year, p.CreatedDate.Month Into Group
Select New With {.Year = Year, .Month = Month, .Count = Group.Count()}).ToList
Run Code Online (Sandbox Code Playgroud)
或者
Dim q2 = (From p In Repository.Table
Group p By key = New With {.Year = p.CreatedDate.Year, .Month = p.CreatedDate.Month} Into Group
Select New With {.Year = key.Year, .Month = key.Month, .Count = Group.Count()}).ToList
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
18683 次 |
最近记录: |