LINQ - 使用"进入"的数据库查询,如何在"之前引用数据"在select子句中?

时间:2014-10-05 18:56:51

标签: linq linq-to-sql linq-to-objects

首先为这个问题的标题道歉 - 我不知道如何更好地表达它。

下面的查询检索2014年1月,2月,3月的产品总销售额,以及这3个月的总计和平均月销售额。 但是,我还想从表StockItems(下面用别名s加入)列标题FreeStockQuantity中检索数据,并在返回的数据中包含该数据。 我原本希望在select子句中添加一行,类似于: 股票= s.FreeStockQuantity 但是这会返回错误: 无法将lambda表达式转换为' string'因为它不是委托类型 名字' s'在当前上下文中不存在

引用一个表格的正确语法是什么在"组d之前由s.ItemID引入g"?

非常感谢您的帮助。我是新来的!

var Sales= from d in cxt.SOPDespatchReceiptLines 

join l in cxt.SOPOrderReturnLines on d.SOPOrderReturnLineID equals l.SOPOrderReturnLineID
join o in cxt.SOPOrderReturns on l.SOPOrderReturnID equals o.SOPOrderReturnID
join s in cxt.StockItems on l.ItemCode equals s.Code
join p in cxt.ProductGroups on s.ProductGroupID equals p.ProductGroupID
join c in cxt.SLCustomerAccounts on o.CustomerID equals c.SLCustomerAccountID
where d.DespatchReceiptDate.Year ==2014
orderby d.SOPOrderReturnLine.ItemCode
//group d by d.SOPOrderReturnLine.ItemCode into g

group d by s.ItemID into g


select new
{

ItemID = g.Key,
Total = g.Sum(d=>d.DespatchReceiptQuantity),
Average = (g.Sum(d=>d.DespatchReceiptQuantity))/3,
Jan = g.Sum(d => d.DespatchReceiptDate.Month==01 ? d.DespatchReceiptQuantity : 0),
Feb= g.Sum(d => d.DespatchReceiptDate.Month==02 ? d.DespatchReceiptQuantity : 0),
Mar= g.Sum(d => d.DespatchReceiptDate.Month==03 ? d.DespatchReceiptQuantity : 0),

};

return Sales;

1 个答案:

答案 0 :(得分:0)

你不太清楚我在评论中的意思。所以尝试添加更多解释样本

第一个变体group new {d, s}

var Sales= from d in cxt.SOPDespatchReceiptLines 

join l in cxt.SOPOrderReturnLines on d.SOPOrderReturnLineID equals l.SOPOrderReturnLineID
join o in cxt.SOPOrderReturns on l.SOPOrderReturnID equals o.SOPOrderReturnID
join s in cxt.StockItems on l.ItemCode equals s.Code
join p in cxt.ProductGroups on s.ProductGroupID equals p.ProductGroupID
join c in cxt.SLCustomerAccounts on o.CustomerID equals c.SLCustomerAccountID
where d.DespatchReceiptDate.Year ==2014
orderby d.SOPOrderReturnLine.ItemCode
//group d by d.SOPOrderReturnLine.ItemCode into g

group new {d,s} by s.ItemID into g

select new
{
    ItemID = g.Key,
    Total = g.Sum(d=>d.d.DespatchReceiptQuantity),
    Average = (g.Sum(d=>d.d.DespatchReceiptQuantity))/3,
    Jan = g.Sum(d => d.d.DespatchReceiptDate.Month==01 ? d.d.DespatchReceiptQuantity : 0),
    Feb= g.Sum(d => d.d.DespatchReceiptDate.Month==02 ? d.d.DespatchReceiptQuantity : 0),
    Mar= g.Sum(d => d.d.DespatchReceiptDate.Month==03 ? d.d.DespatchReceiptQuantity : 0),
    Stock = g.First(s=>s.s)
};

return Sales;

第二种方式by new {s.ItemID, s.FreeStockQuantity}

var Sales= from d in cxt.SOPDespatchReceiptLines 

join l in cxt.SOPOrderReturnLines on d.SOPOrderReturnLineID equals l.SOPOrderReturnLineID
join o in cxt.SOPOrderReturns on l.SOPOrderReturnID equals o.SOPOrderReturnID
join s in cxt.StockItems on l.ItemCode equals s.Code
join p in cxt.ProductGroups on s.ProductGroupID equals p.ProductGroupID
join c in cxt.SLCustomerAccounts on o.CustomerID equals c.SLCustomerAccountID
where d.DespatchReceiptDate.Year ==2014
orderby d.SOPOrderReturnLine.ItemCode
//group d by d.SOPOrderReturnLine.ItemCode into g

group d by new {s.ItemID, s.FreeStockQuantity} into g

select new
{
    ItemID = g.Key.ItemID,
    Total = g.Sum(d=>d.d.DespatchReceiptQuantity),
    Average = (g.Sum(d=>d.d.DespatchReceiptQuantity))/3,
    Jan = g.Sum(d => d.d.DespatchReceiptDate.Month==01 ? d.d.DespatchReceiptQuantity : 0),
    Feb= g.Sum(d => d.d.DespatchReceiptDate.Month==02 ? d.d.DespatchReceiptQuantity : 0),
    Mar= g.Sum(d => d.d.DespatchReceiptDate.Month==03 ? d.d.DespatchReceiptQuantity : 0),
    Stock = k.Key.FreeStockQuantity
};

return Sales;