VB.NET中的Lambda表达式

5

我有一些事情让我非常疯狂...

这些内容与IT技术无关。
    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

这行代码 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)”未定义。

但是我真的看不出它与 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; 
}

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


Lambda表达式 - 哈哈 :) - Sam Wessel
这是被禁止的代码! - 1800 INFORMATION
被禁止的代码?我以为那是 Ruby ? - GEOCHET
5个回答

8
这里所接受的答案可能是错误的,根据您的代码。Chyne 给出了正确的提示:在 VB 中,Lambda 始终具有返回值(不像 C#),不过语句 Lambda 在下一个版本中引入。

同时,您无法在 VB 中使用此代码。请改用常规循环:

For Each p In parents
    p.sub_account_groups = sql_func(p)
Next

VB的下一个版本(自昨天起可作为Beta版使用)将允许编写以下代码:

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

你需要EndSub,对吗?或者他们在Beta1中将其删除了吗? - Richard Anthony Freeman-Hein
@Auxon: 对于像上面那样的一行代码,你不需要它。至少以前是这样的。我尚未测试过测试版:我正在使用OS X。 - Konrad Rudolph

8
Lambda在VB.Net中必须返回一个值,因此您的等号('=')被解释为比较运算符(使lambda返回布尔值),而不是赋值操作。

chyne 是正确的。你的代码在 C# 中可以正常工作。我制作了一个类似的示例来测试它。此外,您不能有多行,因此甚至不能添加 Return。所以你必须使用 ForEach 循环。 - Richard Anthony Freeman-Hein

0

使用 Sub 作为赋值运算符:

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

0

我猜 ag.parent_id = 0 应该改为 Where ag.parent_id == 0


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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接