【发布时间】:2021-11-10 00:32:51
【问题描述】:
我在我的函数中返回一个 IEnumerable,但我现在需要按一个数字 (UitleenbonNR) 对我的结果进行分组。对于其余部分,我希望返回相同的 IEnumerable,而不是列表或其他任何东西,只是与我的 Group by 查询之前相同的 IEnumerable。 在 SQL 中这很容易,但使用 LINQ 我似乎找不到解决方案。
我的工作功能如下:
Public Function GetUitleenbonnen(ByVal bon As String, ByVal ontlener As String, ByVal asset As String) As IEnumerable(Of tblUitleenbon)
dc = New BradyL2S(General.dblocation)
Dim results = From a In dc.tblUitleenbons
If bon <> "" Then
results = From a In results Where a.UitleenbonNaam.Contains(bon)
End If
If ontlener <> "" Then
results = From a In results Where a.UitleenbonOntlenerNaam.Contains(ontlener)
End If
If asset <> "" Then
results = From a In results Where a.UitleenbonAssetNaam.Contains(asset)
End If
results = From a In results Order By a.UitleenbonNR Ascending
Return results
End Function
所以在最后一行,Order By 应该改为 Group By 子句。 我尝试了很多不同的方法,但都没有成功。
results = From a In results Group a By a.UitleenbonNR
或
results = From a In results Group a By a.UitleenbonNR Into anr = Group
或许多其他选项,但都失败了。 有什么建议我可以做一个 Group By 并得到一个 IEnumerable 作为回报?
谢谢
【问题讨论】:
-
他们是怎么失败的?什么错误?
-
将组分配回
results存在问题,因为一旦您将IEnumerable(Of a)分组,您就不能再拥有IEnumerable(Of a),因为根据定义,组是多个事物的倍数。因此,您应该从Dim value = From a In ...开始,而不是分配回结果,然后查看类型值是什么。 VB.Net is notoriously bad with query expression grouping,因此是我的 lambda 解决方案 -
“对于其他人,我想拥有相同的 IEnumerable”是什么意思 - 返回相同的
IEnumerable意味着不执行GroupBy?