c# - How to Create the Left Join by using the group by data -
i have 3 table 1 "allowance
" ,"balance
" , "timeoffrequests
" in these 3 table common columns employeeid
, timeofftypeid
, need requested hours of 1 leave type grouping thier timeofftypeid
, employeeid
table "timeoffrequests
" , , got "timeoffhours
". wrote code like
var query = (from tr in timeoffrequests tr.employeeid == 9 group tr new { tr.employeeid, tr.timeofftypeid } res select new { employeeid = res.key.employeeid, timeofftypeid = res.key.timeofftypeid, totalhours = res.sum(x => x.timeoffhours) }).asenumerable();
now need join these results first table , have employees, , timeofftypes
userallowance
, corresponding timeoffhours
grouped table. getting left joined query wrote below.
var requestresult = (from ua in userallowances join ub in userbalances on ua.employeeid equals ub.employeeid ua.timeofftypeid == ub.timeofftypeid && ua.employeeid == 9 && ua.timeofftype.isdeductabletype == true // leftjoin join rest in query on ua.employeeid equals rest.employeeid penidngrequst penreq in penidngrequst.defaultifempty() penreq.timeofftypeid == ua.timeofftypeid select new employeetimeoffbalancemodel { timeofftypeid = ua.timeofftypeid != null ? ua.timeofftypeid : 0, yearlyallowancehrs = (ua.yearlyallowancehrs != null) ? ua.yearlyallowancehrs : 0, balancehours = ub.balancehrs != null ? ub.balancehrs : 0, pendinghours = (decimal)((penreq != null) ? (penreq.totalhours) : 0), employeeid = ua != null ? ua.employeeid : 0, }).tolist().distinct();
it giving timeofftype
containing in grouped data,even though wrote leftjoin
query using "into" , defaultifempty()
keywords. results becomes like:
and using "linqpad" editor found applying cross
or outer join
instead of "left join" reason.
if remove line of code " penreq.timeofftypeid == ua.timeofftypeid"
showing timeofftypes
cross join
repeatation like
how can achieve left join tables grouped data , showing null values if timeofftypes
didn't having request?
you might want move clause the on equals clause shown below
join rest in query on new { ua.employeeid, ua.timeofftypeid } equals new { rest.employeeid, rest.timeofftypeid } penidngrequst penreq in penidngrequst.defaultifempty()
Comments
Post a Comment