<p>I have something that is driving me absolutely crazy...</p>

<pre><code>    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
</code></pre>

<p>The line <code>parents.ForEach(Function(p) p.sub_account_groups = sql_func(p))</code> has this error...</p>

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

<p>but I really can't see how it is any different from this code from Rob Connery</p>

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

    return parents; 
}
</code></pre>

<p>which compiles perfectly... what am I doing incorrectly?</p>

[{'answer_id': 68795, 'author': 'Jeff Moser', 'author_id': 1869, 'author_profile': 'https://Stackoverflow.com/users/1869', 'pm_score': 0, 'selected': False, 'text': '<p>I haven\'t used VB.NET since moving to C# 3.0, but it seems like it could be a type inference issue. The error is a bit odd since List implements IList, so the assignment should work. You can say "p.ID = 123" for the lambda and things seem to work. </p>\n\n<p>For anyone else interested in looking into it, here is code that you can paste into a new VB.NET console project to demonstrate this issue:</p>\n\n<pre><code>Module Module1\n    Sub Main()\n    End Sub\nEnd Module\n\nClass AccountGroup\n    Public parent_id As Integer\n    Public id As Integer\n    Public sub_account_groups As List(Of AccountGroup)\nEnd Class\nClass AccountRepository\n    Private _repository As AccountRepository\n\n    Public Function GetAccountGroups() As IList(Of AccountGroup)\n        Dim raw_account_groups As IList(Of AccountGroup)\n        raw_account_groups = _repository.GetAccountGroups().ToList()\n        Dim parents = (From ag In raw_account_groups _\n                       Where ag.parent_id = 0 _\n                       Select ag).ToList()\n        parents(0).sub_account_groups = (From sag In raw_account_groups _\n                                         Where sag.parent_id = 0 _\n                                         Select sag).ToList()\n\n        Dim sql_func As Func(Of AccountGroup, List(Of AccountGroup)) = Function(p) _\n                                                                            (From sag In raw_account_groups _\n                                                                             Where sag.parent_id = p.id _\n                                                                             Select sag).ToList()\n\n\n\n        parents.ForEach(Function(p) p.sub_account_groups = sql_func(p))\n        Return parents\n    End Function\n End Class\n</code></pre>\n'}, {'answer_id': 892437, 'author': 'Tore Nestenius', 'author_id': 68490, 'author_profile': 'https://Stackoverflow.com/users/68490', 'pm_score': 0, 'selected': False, 'text': '<p>I guess <code>ag.parent_id = 0</code> should be Where <code>ag.parent_id == 0</code>?</p>\n'}, {'answer_id': 892813, 'author': 'chyne', 'author_id': 25157, 'author_profile': 'https://Stackoverflow.com/users/25157', 'pm_score': 3, 'selected': False, 'text': "<p>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.</p>\n"}, {'answer_id': 892842, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 3, 'selected': False, 'text': "<p>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.</p>\n\n<p>In the meantime, you simply can't use this code in VB. Use a regular loop instead:</p>\n\n<pre><code>For Each p In parents\n    p.sub_account_groups = sql_func(p)\nNext\n</code></pre>\n\n<p>The next version of VB (available as a Beta since yesterday) would allow the following code to be written:</p>\n\n<pre><code>parents.ForEach(Sub (p) p.sub_account_groups = sql_func(p))\n</code></pre>\n"}, {'answer_id': 2892799, 'author': 'BSalita', 'author_id': 317797, 'author_profile': 'https://Stackoverflow.com/users/317797', 'pm_score': 0, 'selected': False, 'text': '<p>Use Sub for assignment operator: </p>\n\n<pre><code>parents.ForEach(Sub(p) p.sub_account_groups = sql_func(p))\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/67916', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/10479/']