c# - Linq to entities : Many to many in one single select -
i have domain:
public class user { public long id { get; set; } public string login { get; set; } public string password { get; set; } } public class application { public long id { get; set; } public string name { get; set; } } public class userapplications { [foreignkey("user")] public long userid { get; set; } public user user { get; set; } [foreignkey("application")] public long applicationid { get; set; } public application application { get; set; } public datetime lastconnection { get; set; } }
i want make select returns that:
list of select new { user = user, applications = applications // list of user's applications }
i try:
from u in users join ua in userapplications on u.id equals ua.userid userapplications ua in userapplications.defaultifempty() join in applications on ua.applicationid equals a.id applications select new { user = u, applications = applications }
but repeats user each application.
i know can in 2 select statements, dont want that.
how can that?
i not remember if entity frameworks groupby
based on entity object (and extract it's id
behind scene , replace things fits , like); code works case:
var q = uapp in cntxt.userapplications group uapp uapp.userid g select new { userid = g.key, applications = g.select(x => x.application) };
and if willing have user
extracted:
var q2 = uapp in cntxt.userapplications group uapp uapp.userid g let u = users.first(x => x.id == g.key) select new { user = u, applications = g.select(x => x.application) };
assuming writing query against entity framework context - , not trying linq objects query.
Comments
Post a Comment