LINQ-使用“into”进行数据库查询,如何引用select子句中“into”之前的数据?

LINQ-使用“into”进行数据库查询,如何引用select子句中“into”之前的数据?,linq,linq-to-sql,linq-to-objects,Linq,Linq To Sql,Linq To Objects,首先,我为这个问题的标题道歉——我不知道如何更好地表达它 下面的查询检索2014年1月、2月、3月的产品总销售额,以及这3个月的总销售额和平均月销售额。 但是,我还想从下面使用别名s列标题FreeStockQuantity连接的StockItems表中检索数据,并将其包含在返回的数据中。 我希望在select子句中添加一行类似于: 库存=s.自由库存数量 但是,这将返回错误: 无法将lambda表达式转换为类型“string”,因为它不是委托类型 名称“s”在当前上下文中不存在 将d组前面的表由

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

下面的查询检索2014年1月、2月、3月的产品总销售额,以及这3个月的总销售额和平均月销售额。 但是,我还想从下面使用别名s列标题FreeStockQuantity连接的StockItems表中检索数据,并将其包含在返回的数据中。 我希望在select子句中添加一行类似于: 库存=s.自由库存数量 但是,这将返回错误: 无法将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;

我想你不太明白我在评论中的意思。因此,请尝试添加更多解释示例

第一变异群新{d,s}

通过新{s.ItemID,s.FreeStockQuantity}的第二种方式


您需要简单地将s添加到组值中,例如通过s.ItemID将组new{d,s}添加到g中,或者通过新的{s.ItemID,s.FreeStockQuantity}将其添加到键组d中,以获得答复。我尝试使用组new{d,s}由s.ItemID转换为g,这给了我一个错误:无法将lambda表达式转换为string类型,因为它不是委托类型AnonymousType1不包含DispatchReceiptQuantity的定义,并且找不到接受AnonymousType1类型第一个参数的扩展方法DispatchReceiptQuantity是否缺少using指令或程序集引用?当我尝试将新的{s.ItemID,s.FreeStockQuantity}添加到g中的第二个建议组d时,查询运行时没有出现错误,但是,当我随后插入Stock=s.FreeStockQuantity行时,它给了我以前遇到的相同类型的错误:无法将lambda表达式转换为类型“string”,因为它不是委托类型,名称“s”在当前上下文中不存在。非常感谢您的帮助。我无法使用第一种方法。我仍然遇到一个错误,无法将lambda表达式转换为“string”类型,因为它不是委托类型,但是第二种方法工作得很好。
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;
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;