sql server - Sql converted to Linq - Left Join, Group, Min and Sum -


i have simple piece of sql need...

select min(pb.id) id, min(pb.quantity) requested, sum(pbi.quantity) total pickbatchitems pb left join pickbatchitemlocations pbi on pb.id = pbi.pickbatchitemid group pb.id 

results in...

id, requested, total  1    100       null  2    200       165  3    200       null  

this want need in linq.

so far have...

var pick = (from pb in db.pickitems            join pbi in db.pickitemlocations on pb.id equals pbi.pickbatchitemid tempjoin            reslist in tempjoin.defaultifempty()            pb.id == ipickitemid            group reslist pb.id g            select new            {                id = g.key,                requestedquantity = g.min(???????????????????????),                sentquantity = g.sum(a => a.quantity == null ? 0 : a.quantity),            }).firstordefault(); 

how can requestedquantity?

update:

thanks 'david b' have answer:

var pick = (from pb in db.pickbatchitems             join pbi in db.pickbatchitemlocations on pb.id equals pbi.pickbatchitemid tempjoin             reslist in tempjoin.defaultifempty()             pb.id == ipickitemid             group new { pickitem = pb, pickitemlocation = reslist } pb.id g             select new             {                 id = g.key,                 requestedquantity = g.min(a => a.pickitem.quantity),                 sentquantity = g.sum(a => a.pickitemlocation.quantity == null ? 0 : a.pickitemlocation.quantity),             }).firstordefault(); 

this joins , returns 2 tables if pickbatchitemlocations table empty.

assuming pickitems.id primary key, seems joining first step road need not traveled.

from pb in db.pickitems pb.id = ipickitemid //this wasn't in original sql select new {   id = pb.id,   requestedquantity = pb.quantity,   sentquantity = pb.pickitemlocations.sum(pbi => pbi.quantity) }; 

if still want take long road, this:

group new {pickitem = pb, pickitemlocation = reslist } pb.id g select new {   id = g.key,   requestedquantity = g.min(x => x.pickitem.quantity),   sentquantity = g.sum(x => x.pickitemlocation.quantity ?? 0) } 

Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -