0

请帮我在 LINQ-Enitites 中转换这个 SQL 查询

select e.RowMeterialName,f.RowUnitName,*,isnull((b.Quantity - (Select SUM(a.Quantity) from PurchaseDetail a
                    join PurchaseMaster c on a.PurchaseId=c.PurchaseId
                    where a.RawMetId=b.RawMetrialId and c.PurchaseOrderId=b.PurchaseOrderId)),b.Quantity) as Remain 
                    from PurchaseOrderDetail b
                    join RawMeterialMaster e on b.RawMetrialId=e.RawMetId
                    join RawUnitMaster f on e.RawUnitId=f.RowUnitId
                    where b.PurchaseOrderId=@PurchaseOrderId

请帮我。

4

2 回答 2

0

这是一个很长的镜头,但您可以尝试一下:

PurchaseDetail
.Join
(
    PurchaseMaster,
    x=>x.PurchaseId,
    x=>x.PurchaseId,
    (detail,master)=>new {detail,master}
)
.Join
(
    PurchaseOrderMaster,
    x=>x.PurchaseOrderId,
    x=>x.PurchaseOrderId,
    (joined,ordermaster)=>new{detail=joined.detail,master=joined.master,ordermaster}
)
.Select
(
    x=>
    new{detail.Quantity,detail.RawMetId}
)
.GroupBy(x=>x.RawMetId)
.Select
(
    x=>
    new
    {
        RawMetId=x.Key,
        Quantity=x.Sum(z=>z.Quantity)
    }
)
.Join
(
    PurchaseOrderDetail,
    x=>x.RawMetrialId,
    x=>x.RawMetId,
    (subquery,orderdetail)=>new{subquery,Remain=subquery.Quantity - orderdetail.Quantity}
)
于 2013-09-10T08:08:13.707 回答
0

谢谢大家,但我找到了这个查询的结果

from b in db.PurchaseOrderDetails
                        select new
                        {
                            b.RawMeterialMaster.RowMeterialName,
                            PurOrderDetailId = b.PurOrderDetailId,
                            PurchaseOrderId = b.PurchaseOrderId,
                            RawMetrialId = b.RawMetrialId,
                            DueOn = b.DueOn,
                            Quantity = b.Quantity,
                            ItemRate = b.ItemRate,
                            MetrialUnitsId = b.MetrialUnitsId,
                            Remark = b.Remark,
                            RowUnitName = b.RawMeterialMaster.RawUnitMaster.RowUnitName,
                            ReOrderQuantity = (decimal?)b.RawMeterialMaster.ReOrderQuantity,
                            Remain = ((System.Decimal?)b.Quantity -
                              (from a in db.PurchaseDetails
                               where
                                a.RawMetId == b.RawMetrialId &&
                                a.PurchaseMaster.PurchaseOrderId == b.PurchaseOrderId
                               select new
                               {
                                   a.Quantity
                               }).Sum(p => p.Quantity) ?? (System.Decimal?)b.Quantity)
                        };
于 2013-09-18T08:20:19.513 回答