vb.net中的lambda表达式

use*_*479 5 linq vb.net

我有些东西让我绝对疯狂......

    Public Function GetAccountGroups() As IList(Of AccountGroup)
        Dim raw_account_groups As IList(Of AccountGroup)
        raw_account_groups = _repository.GetAccountGroups().ToList()
        Dim parents = (From ag In raw_account_groups _
                      Where ag.parent_id = 0 _
                      Select ag).ToList()

        parents(0).sub_account_groups = (From sag In raw_account_groups _
                               Where sag.parent_id = 0 _
                                Select sag).ToList()

        Dim sql_func As Func(Of AccountGroup, List(Of AccountGroup)) = Function(p) _
                                                                      (From sag In raw_account_groups _
                                                                       Where sag.parent_id = p.id _
                                                                       Select sag).ToList()

        parents.ForEach(Function(p) p.sub_account_groups = sql_func(p))

        Return parents
    End Function
Run Code Online (Sandbox Code Playgroud)

行"parents.ForEach(Function(p)p.sub_account_groups = sql_func(p))"出现此错误...

没有为类型'System.Collections.Generic.IList(Of st.data.AccountGroup)'和'System.Collections.Generic.List(Of st.data.AccountGroup)'定义Operator'='.

但我真的看不出它与Rob Connery的代码有什么不同

public IList<Category> GetCategories() {
IList<Category> rawCategories = _repository.GetCategories().ToList(); var parents = (from c in rawCategories 
where c.ParentID == 0
select c).ToList();
 parents.ForEach(p =>
{
p.SubCategories = (from subs in rawCategories
where subs.ParentID == p.ID
select subs).ToList();
});

return parents; 
}
Run Code Online (Sandbox Code Playgroud)

它编译得很完美......我做错了什么?

chy*_*yne 8

VB.Net中的Lambda必须返回一个值,所以你的等号('=')被解释为比较(这样lambda返回一个布尔值),而不是赋值.


Kon*_*lph 8

根据您的代码,这里接受的答案可能是错误的.chyne给出了正确的线索:VB中的lambdas总是有返回值(与C#不同),语句lambdas虽然在下一个版本中引入.

在此期间,您根本无法在VB中使用此代码.改为使用常规循环:

For Each p In parents
    p.sub_account_groups = sql_func(p)
Next
Run Code Online (Sandbox Code Playgroud)

VB的下一个版本(自昨天起作为测试版提供)将允许编写以下代码:

parents.ForEach(Sub (p) p.sub_account_groups = sql_func(p))
Run Code Online (Sandbox Code Playgroud)


Jef*_*ser 0

自从转向 C# 3.0 以来,我就没有使用过 VB.NET,但看起来这可能是一个类型推断问题。由于 List 实现了 IList,因此该错误有点奇怪,因此该分配应该有效。你可以对 lambda 说“p.ID = 123”,一切似乎都有效。

对于有兴趣研究它的其他人,您可以将以下代码粘贴到新的 VB.NET 控制台项目中来演示此问题:

Module Module1
    Sub Main()
    End Sub
End Module

Class AccountGroup
    Public parent_id As Integer
    Public id As Integer
    Public sub_account_groups As List(Of AccountGroup)
End Class
Class AccountRepository
    Private _repository As AccountRepository

    Public Function GetAccountGroups() As IList(Of AccountGroup)
        Dim raw_account_groups As IList(Of AccountGroup)
        raw_account_groups = _repository.GetAccountGroups().ToList()
        Dim parents = (From ag In raw_account_groups _
                       Where ag.parent_id = 0 _
                       Select ag).ToList()
        parents(0).sub_account_groups = (From sag In raw_account_groups _
                                         Where sag.parent_id = 0 _
                                         Select sag).ToList()

        Dim sql_func As Func(Of AccountGroup, List(Of AccountGroup)) = Function(p) _
                                                                            (From sag In raw_account_groups _
                                                                             Where sag.parent_id = p.id _
                                                                             Select sag).ToList()



        parents.ForEach(Function(p) p.sub_account_groups = sql_func(p))
        Return parents
    End Function
 End Class
Run Code Online (Sandbox Code Playgroud)