0

我在将 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 and d.ModifyStatus != 'D'

4

1 回答 1

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;
}
于 2013-02-01T09:03:32.383 回答