Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lambda expressions in vb.net

Tags:

vb.net

linq

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

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

Operator '=' is not defined for types 'System.Collections.Generic.IList(Of st.data.AccountGroup)' and 'System.Collections.Generic.List(Of st.data.AccountGroup)'.

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?

like image 768
user10479 Avatar asked Sep 15 '08 23:09

user10479


People also ask

What is a lambda expression example?

Lambda expressions basically express instances of functional interfaces (An interface with single abstract method is called functional interface. An example is java.lang.Runnable).

What is lambda expression explain?

A lambda expression is a short block of code which takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.

What is lambda expression in .NET framework?

Lambda expressions in C# are used like anonymous functions, with the difference that in Lambda expressions you don't need to specify the type of the value that you input thus making it more flexible to use. The '=>' is the lambda operator which is used in all lambda expressions.

Why is lambda expression used?

The Lambda expression is used to provide the implementation of an interface which has functional interface. It saves a lot of code. In case of lambda expression, we don't need to define the method again for providing the implementation.


1 Answers

Lambda's in VB.Net have to return a value, so your equal sign ('=') is being intepreted as a comparison (so that the lambda returns a boolean), rather than an assignment.

like image 163
chyne Avatar answered Nov 15 '22 06:11

chyne