c# - How to sort in EF parents by number of children of children of children? -
i have teams of coders of projects of tasks.
how sort teams number of tasks in entity framework using method syntax , code first approach (probably without loading whole collection contents of children of children, need sort only)?
public class myappcontext : dbcontext { public dbset<cteam> teams { get; set; } // ...etc }
[table("team")] public class cteam { public int id { get; set; } [maxlength(128)] public string title { get; set; } [display(name = "coders")] public virtual icollection<ccoder> coders { get; set; } } [table("coder")] public class ccoder { public int id { get; set; } [maxlength(128)] public string name { get; set; } public virtual icollection<cproject> projects { get; set; } public int teamid { get; set; } public virtual cteam team { get; set; } } // cproject , ctask
optionally, if give hint how raw sql should this, too. thanks!
update
in project relationships builder -> newbuildingobject -> newbuildinghouse -> newbuildinglayout
for viewer's convinience, teodor ivanov's answer:
context.teams .orderby(x => x.coders .selectmany(y => y.projects) .selectmany(y => y.tasks) .count());
made ef generate code:
select [project1].[id] [id], [project1].[title] [title] ( select [extent1].[id] [id], [extent1].[title] [title], (select count(1) [a1] [dbo].[newbuildingobject] [extent2] inner join [dbo].[newbuildinghouse] [extent3] on [extent2].[id] = [extent3].[objectid] inner join [dbo].[newbuildinglayout] [extent4] on [extent3].[id] = [extent4].[houseid] [extent1].[id] = [extent2].[builderid]) [c1] [dbo].[builder] [extent1] ) [project1] order [project1].[c1] asc
and yacoub massad's answer:
var teams = context.teams .orderby(team => team.coders.selectmany(coder => coder.projects.selectmany(project => project.tasks)).count());
made code:
select [project1].[id] [id], [project1].[title] [title] ( select [extent1].[id] [id], [extent1].[title] [title], (select count(1) [a1] [dbo].[newbuildingobject] [extent2] inner join (select [extent3].[objectid] [objectid] [dbo].[newbuildinghouse] [extent3] inner join [dbo].[newbuildinglayout] [extent4] on [extent3].[id] = [extent4].[houseid] ) [join1] on [extent2].[id] = [join1].[objectid] [extent1].[id] = [extent2].[builderid]) [c1] [dbo].[builder] [extent1] ) [project1] order [project1].[c1] asc
you can try like
context.teams .orderby(x => x.coders .selectmany(y => y.projects) .selectmany(y => y.tasks) .count());
Comments
Post a Comment