vb.net中的lambda表达式 [英] lambda expressions in vb.net

查看:189
本文介绍了vb.net中的lambda表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些东西让我疯狂极了...

I have something that is driving me absolutely crazy...

    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))"行出现此错误...

The line "parents.ForEach(Function(p) p.sub_account_groups = sql_func(p))" has this error...

运算符'='.

但是我真的看不出它与Rob Connery的这段代码有何不同

but I really can't see how it is any different from this code from 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; 
}

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

which compiles perfectly... what am I doing incorrectly?

推荐答案

根据您的代码,此处接受的答案可能是错误的. chyne给出了正确的线索:VB中的lambda始终具有返回值(与C#不同),但是在下一版本中引入了语句lambda.

The accepted answer here is probably wrong, based on your code. chyne has given the correct clue: lambdas in VB always have return values (unlike in C#), statement lambdas are introduced in the next version though.

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

In the meantime, you simply can't use this code in VB. Use a regular loop instead:

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

下一个版本的VB(自昨天开始提供Beta版)将允许编写以下代码:

The next version of VB (available as a Beta since yesterday) would allow the following code to be written:

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

这篇关于vb.net中的lambda表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆