Search code examples
entity-frameworklinqjoinlinq-group

Linq Group Join Items into List


It's a master detail scenario, where each TABLE1 has multiple rows from TABLE2 assigned and I want to do something lihe this:

From a In TABLE1
Group Join c In TABLE2 On c.ID2 Equals a.ID Into Group
Select New ViewModel1 With {
    .COLUMN1 = a.COLUMN1,
    .COLUMN2 = a.COLUMN2,
    .SUBTABLE = New ViewModel2 With {
        .SUBCOLUMN1 = c.SUBCOLUMN1,
        .SUBCOLUMN2 = c.SUBCOLUMN2,
    }
}

Is this somehow possible?


Solution

  • Do you mean this:

            var foobars = from foo in foolist
                          join bar in barlist on foo.Fooo equals bar.FooBar into t
                          select new
                          {
                              foo.Baar,
                              barbar = from bar in t
                                       select new { bar.FooBar, bar.BarFoo }
                          };
    

    That would be (approximately) how you do the query you described.

    Sorry, I had to reread the question a few times to get that it was being mapped to the first element.