-1
我使用SQL 2014和2013以及EF4。如何在Linq查询中加入4个表格
我有4个表中的数据库:
SupplierPurchaseAlarm
Order
OrderDetail
Product
我想加入这些表格并获得NewOrderCount
,AllSellProductCount
和SumOf Price
in orderDetail
。
我写了这个代码,但它不为我工作:
public ShowAllSupplierAlarmInfo GetAllMsgAndAlarmCount(long suplierId)
{
try
{
var supplierPurchaseList = _db.Context.SuplierPurchaseAlarms.Where(x => x.SuplierId == suplierId).AsQueryable();
var query = from supPurchaseMsg in supplierPurchaseList
join ord in _db.Context.Orders on supPurchaseMsg.OrderId equals ord.OrderID
join ordDetail in _db.Context.OrderDetails on ord.OrderID equals ordDetail.OrderID
join prd in _db.Context.Products on ordDetail.ProductID equals prd.ProductID
where prd.SupplierID==supPurchaseMsg.SuplierId
where
ord.IsPayed && ord.CurentOrderState == (int) EnumCurrentOrderState.ProductIsPreparingToSend ||
ord.CurentOrderState == (int) EnumCurrentOrderState.TheProductIsWaitingForApprovalBySuppliers
group ordDetail by ord
into j
select new
{
OrderCount = j.GroupBy(x => x.Order.OrderID).Count(),
SellProductCount = j.Sum(x => x.Quantity),
AllOrderCount = j.Count(x => x.Order.IsPayed),
profit = j.Sum(x=>x.UnitPrice*x.Quantity)
};
var res = new ShowAllSupplierAlarmInfo()
{
PurchaseAlertCount = supplierPurchaseList.Count(x => x.TypeId==(int)AlarmTypeId.Warning)
};
foreach (var prd in query)
{
res.SellCount += prd.SellProductCount;
res.NewOrderCount += prd.OrderCount;
res.AllOrderCount += prd.AllOrderCount;
res.Profit = prd.profit;
}
return res;
}