I want to convert this query to Linq. I left joined BLTb to BMTb and CbTb. I did not use BLTb in group by clause. I can't convert this query to Linq because I can't sum BLTb.IsAprv. I just can sum the fields whose tables are in group by clause in linq.
Could someone please tell me how I can convert the below query to Linq.
Note:
"BLTb.BLCod" is not the PK of the table "BLTb".
"BMTb.Id" is not the PK of the table " BMTb"
SELECT BMTb.BCod, CBTb.CmCod,
CASE WHEN
SUM(CASE WHEN ISNULL(BMTb.IsAprv, 0) = 0 THEN 1 ELSE 0 END) +
SUM(CASE WHEN BLTb.IsAprv = 0 THEN 1 ELSE 0 END)
> 0 then 0 else 1 end AS IsAprv
FROM BMTb
INNER JOIN CBTb ON BMTb.CBCod = CBTb.CBCod
LEFT OUTER JOIN BLTb ON BMTb.Id = BLTb.BLCod
GROUP BY BCod
Linq:
var BMTb = (from m in db.BMTbs.AsQueryable()
select new
{
isAprove=m.IsAprv==null?1:m.IsAprv==false?1:0,
m.CBCod,
m.IsAprv,
m.BCod,
m.Id
}
);
var BLTb = (from l in db.BLTbs.AsQueryable()
select new
{
l.BMCod,
isAprove= l.IsAprv==false?1:0
}
);
var AproveCB = (from m in BMTb.AsQueryable()
join c in db.CBTbs.AsQueryable() on m.CBCod equals c.CBCod
join l in BLTb.AsQueryable() on m.Id equals l.BMCod into lL
from lLeft in lL.DefaultIfEmpty()
group new { m, c } by new {m.BCod,c. CmCod, }
into grp
select new
{
sum1=grp.Sum(gp=>gp.m.isAprove)/*+grp.Sum(gp => gp.isAprove) */> 0?0:1,
BCod=grp.Key.BCod,
CmCod=grp.Key.CmCod,
}
);