sql server - C# Linq Lambda Left Outer Join -
i need create left outer join
in linq lambda syntax. sql trying create linq equivalent of is:
select distinct p.partnum partnum, p.shortchar01 skutype, vv.vendorid vendorcode, p.partdescription description, p.company company part p (nolock) inner join partplant pp on p.company = pp.company , p.partnum = pp.partnum left outer join vendor vv on pp.vendornum = vv.vendornum p.refcategory = @refcategory
so can see simple query joining few tables. issue happen there no vendor still want rest of information hence left outer join.
my current attempt recreate is:
_uow.partservice .get() .where(p => p.refcategory.equals(level2)) .join(_uow.partplantservice.get(), p => new { p.partnum, p.company }, pp => new { pp.partnum, pp.company }, (p, pp) => new { part = p, partplant = pp }) .groupjoin(_uow.vendorservice.get(), pprc => pprc.partplant.vendornum, v => v.vendornum, (pprc, v) => new { pprc = pprc, v = v });
i aware select isn't returning same fields @ moment. have ignored trying ensure getting correct values first.
the sql query returns 41 records 1 record having null vendor. linq query returns 40 records not returning 1 null vendor. have tried using groupjoin()
, defaultifempty()
cannot work.
any appreciated.
from comment , links user2321864, managed working follows:
_uow.partservice.get().where(p => p.refcategory.equals(level2)) .join(_uow.partplantservice.get(), p => new { p.partnum, p.company }, pp => new { pp.partnum, pp.company }, (p, pp) => new { part = p, partplant = pp }) .groupjoin(_uow.vendorservice.get(), pprc => pprc.partplant.vendornum, v => v.vendornum, (pprc, v) => new { pprc = pprc, v = v }) .selectmany(y => y.v.defaultifempty(), (x, y) => new { pprc = x.pprc, vendor = y }) .select(r => new level2parts() { companycode = r.pprc.part.company, description = r.pprc.part.partdescription, partnum = r.pprc.part.partnum, skutype = r.pprc.part.shortchar01, vendorcode = r.vendor.vendorid }) .distinct();
wiki
Comments
Post a Comment