我在将SQL转换为LINQ语法时遇到问题。 这是我的查询。
select ISNULL(Sum(b.Total),0) as 'TotalOP', isnull(Sum(e.Total),0) as 'TotalLPB',
isnull(Sum(b.Total),0) - isnull(Sum(e.Total),0) as 'Difference'
from OP a
left join OP_Detail b on a.ID_Ps_OP = b.ID_Ps_OP
left join LPB_OP c on a.ID_Ps_OP = c.ID_Ps_OP
left join LPB d on c.ID_Ps_LPB = d.ID_Ps_LPB and d.ModifyStatus != 'D'
left join LPBDetail e on d.ID_Ps_LPB = e.ID_Ps_LPB
where a.ModifyStatus != 'D'
该查询在SQL中运行良好。
至于信息:
1. OP_Detail包含OP的商品。它有一个名为Total的字段,其中包含OP中的交易金额
2. LPB_OP是LPB和OP之间的连接器。
3. LPB_Detail包含LPB的货物。它有一个名为Total的字段,其中包含LPB中的交易金额。
是否有任何linq语法可以采用这些sum,isnull和left join问题?
特别是行:
左边连接LPB d on c.ID_Ps_LPB = d.ID_Ps_LPB和d.ModifyStatus!='D'
答案 0 :(得分:1)
也许你可以尝试这样的事情:
var lstQuery =
(from A in OP
from B in OP_Detail
from C in LBP_OP
from D in LPB
from E in LPBDetail
where
A.ID_Ps_OP = B.ID_Ps_OP &&
A.ID_Ps_OP = C.ID_Ps_OP &&
((C.ID_Ps_LPB = D.ID_Ps_LPB) && (D.ModifyStatus != 'D')) &&
D.ID_Ps_LPB = E.ID_Ps_LPB
select new
{
A, B, C, D, E
}).ToList();
double dTotalOP = 0;
double dTotalLPB = 0;
double dDifference = 0;
foreach (var record in lstQuery)
{
dTotalOp += record.B == null ? 0 : record.B.Total;
dTotalLPM += record.E == null ? 0 : record.E.Total;
dDifference += dTotalOp - dTotalLPM;
}