linq - Query that Groups Parent, then Child, when fetching from IQueryable<GrandChild> -
lets imagine below our info model. want query toys have results returned such can next psuedo code.
foreach (var parent in parents) { console.writeline(parent.name); foreach (var kid in parent.children) { console.writeline(child.name); foreach (var toy in child.toys) { console.writeline(toy.name); } } }
the info model:
public class parent { public guid parentid { get; set; } public string name { get; set; } public icollection<child> children { get; set; } } public class kid { public guid childid { get; set; } public guid parentid { get; set; } public parent parent { get; set; } public string name { get; set; } public icollection<toy> toys { get; set; } } public class toy { public guid toyid { get; set; } public guid childid { get; set; } public kid child { get; set; } public string name { get; set; } public bool iscool { get; set; } }
i've tried doing grouping when seek iterate grouping see key doesn't have properties on can't parent.name or child.name.
thanks in advance.
you can do:
var query = p in parents c in p.children t in c.toys select new { parent = p.name, kid = c.name, toy = t.name } foreach (var in query) { // write values. }
in effect, uses selectmany.
edit
after comment, think you're looking not grouping. if you've got parent
object in fact children
grouped within of it, because each parent has own collection. maybe you're looking way populate collections? done by
var query = p in parents.include(x => x.children.select(c => c.toys));
linq linq-to-entities ef-code-first code-first
No comments:
Post a Comment