var query1 = from dailyStocktakeSettingLine in db.DailyStocktakeSettingLines
where dailyStocktakeSettingLine.DailyStocktakeSettingID == dailyStocktakeSettingID
select new {
ID = dailyStocktakeSettingLine.ID,
ProductID = dailyStocktakeSettingLine.ProductID
};
var query2 = from stockMovement in db.StockMovements
where stockMovement.CentreID == centreID && stockMovement.StockStateType == StockStateTypeEnum.Available
group stockMovement by new { stockMovement.ProductID } into grp
select new {
ProductID = grp.Key.ProductID,
Quantity = grp.Sum(e => e.Quantity)
};
var query3 = from stockSnapshotLine in db.StockSnapshotLines
join stockSnapshot in db.StockSnapshots on stockSnapshotLine.StockSnapshotID equals stockSnapshot.ID
where stockSnapshot.CentreID == centreID && stockSnapshotLine.StockStateType == StockStateTypeEnum.Available
group stockSnapshotLine by new { stockSnapshotLine.ProductID } into grp
select new {
ProductID = grp.Key.ProductID,
Quantity = grp.Sum(e => e.Quantity)
};
var query4 = from q1 in query1
join q2 in query2 on q1.ProductID equals q2.ProductID into q1_q2
from q2 in q1_q2.DefaultIfEmpty()
join q3 in query3 on q1.ProductID equals q3.ProductID into q1_q3
from q3 in q1_q3.DefaultIfEmpty()
join product in db.Products on q1.ProductID equals product.ID
join unit in db.Units on product.SmallUnitID equals unit.ID
select new DailyStocktakeForAddLineData {
ID = q1.ID,
ProductID = product.ID,
ProductName = product.Name,
Quantity = (q2 == null ? 0 : q2.Quantity) + (q3 == null ? 0 : q3.Quantity),
SmallUnitName = unit.Name
};