【问题标题】:lambda expressions in vb.netvb.net 中的 lambda 表达式
【发布时间】:2020-05-27 09:03:06
【问题描述】:

我有一些让我发疯的事情......

    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; 
}

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

【问题讨论】:

  • "lambada 表达式" - 哈哈 :)
  • 这是禁止密码!
  • 禁止代码?我以为那是 Ruby?

标签: vb.net linq


【解决方案1】:

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

【讨论】:

  • chyne 是正确的。您的代码在 C# 中可以正常工作。我做了一个类似的例子来测试它。你也不能有多行,所以你甚至不能添加一个返回。所以你必须使用 ForEach 循环。
【解决方案2】:

根据您的代码,此处接受的答案可能是错误的。 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 中删除了它?
  • @Auxon:你不需要它用于上面的单行。至少,你不习惯。我还没有测试测试版:我使用的是 OS X。
【解决方案3】:

自从迁移到 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

【讨论】:

    【解决方案4】:

    使用 Sub 作为赋值运算符:

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

    【讨论】:

      【解决方案5】:

      我猜ag.parent_id = 0 应该在哪里ag.parent_id == 0?

      【讨论】:

        猜你喜欢
        • 2010-11-12
        • 1970-01-01
        • 2011-05-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        • 1970-01-01
        相关资源
        最近更新 更多