tags:

views:

78

answers:

2

I'd like to return an object with the following signature

class AnonClass{
    string Name {get;}
    IEnumerable<Group> Groups {get;}
}

I have tried the following query, but g only returns a single entity, not all the joined entities

var q = from t in dc.Themes
join g in dc.Groups on t.K equals g.ThemeK 
select new {t.Name, Groups = g};
return q.ToArray();

but this returns

class AnonClass{
   string Name {get;}
   Group Groups{get;}
}

What is the correct linq query to use?

A: 

I think you want "join into" instead of just "join":

var q = from t in dc.Themes
        join g in dc.Groups on t.K equals g.ThemeK into groups
        select new { t.Name, Groups=groups };

(That's completely untested, however - worth a try, but please verify carefully!)

Jon Skeet
+1  A: 

If you have the Foreign Key set up correctly, then it should be:

var q = from t in dc.Themes
select new {t.Name, Groups = t.Groups};
James Curran
I had something like this earlier, but the Groups property gets lazy loaded, resulting in loads of sql calls when I try to enumerate it.
mcintyre321