c# - Mapping one to many relationships in group by statement -
there query:
const string query = @" select * [users] u left join [userroles] ur on [ur].[userid] = [u].[userid] left join [roles] r on [r].[roleid] = [ur].[roleid] left join [externallogins] el on [el].[userid] = [u].[userid]; "; this maps data variable, array of next type:
userroleloginrow { role = role, user = user, userlogininforow = userlogin } which output data should grouped user , mapped user entity:
public sealed class user : iuser<string> { public icollection<userlogininfo> userlogininfos; public icollection<role> roles; } i can achieve code:
var users = data.groupby(t => t.user, (key, value) => { key.roles = new collection<role>(value.select(v=>v.role).tolist()); return key; }); but looks me mess, because have create collections myself, think casting , check null references (if select userlogininfomodel database , map userlogininfo).
is there cleaner way achieve (and what's other way)?
ty.
Comments
Post a Comment