Linq 组和聚合
Linq Group and Aggregate
我正在尝试为以下 TSQL 创建 LINQ 查询:
SELECT [TenantID]
,[ABC]
,COUNT(*) [TotalSKU], SUM([QtyOnHand]) AS [SOH],SUM([QtyOnHand]*[Price]) AS [SOH_Value]
,SUM ([QtyOnOrder]) AS SOO
,SUM ([QtyOnOrder]*[Price]) AS [SOO_Value]
FROM [OMS_StockStatus] GROUP BY [TenantID], [ABC]
到目前为止我做到了:
var res = from a in db.OMS_StockStatus
group a by new { a.TenantID, a.ABC } into g
select new ABCDashboard { TenantID = g.Key.TenantID, ABC = g.Key.ABC, TotalSKU = g.Count(),
SOH = g.Sum( t => t.QtyOnHand), SOH_Value = g.Aggregate((c,d) => decimal(c.QtyOnHand) * d.Price)};
但出现以下错误:
'System.Linq.IGrouping' does not contain a definition for 'Aggregate' and the best extension method overload 'System.Linq.Enumerable.Aggregate(System.Collections.Generic.IEnumerable, System.Func)' has some invalid arguments
这是我的实体定义:
public partial class ABCDashboard
{
public short TenantID { get; set; }
public string ABC { get; set; }
public double TotalSKU { get; set; }
public Nullable<double> SOH { get; set; }
public Nullable<decimal> SOH_Value { get; set; }
public Nullable<double> SOO { get; set; }
public Nullable<decimal> SOO_Value { get; set; }
}
在OMS_StockStatus中,QtyOnHand是双倍的?价格翻倍?
用 .. SOH_Value = g.Sum(t => ((decimal)t.QtyOnHand) * t.Price)
我正在尝试为以下 TSQL 创建 LINQ 查询:
SELECT [TenantID]
,[ABC]
,COUNT(*) [TotalSKU], SUM([QtyOnHand]) AS [SOH],SUM([QtyOnHand]*[Price]) AS [SOH_Value]
,SUM ([QtyOnOrder]) AS SOO
,SUM ([QtyOnOrder]*[Price]) AS [SOO_Value]
FROM [OMS_StockStatus] GROUP BY [TenantID], [ABC]
到目前为止我做到了:
var res = from a in db.OMS_StockStatus
group a by new { a.TenantID, a.ABC } into g
select new ABCDashboard { TenantID = g.Key.TenantID, ABC = g.Key.ABC, TotalSKU = g.Count(),
SOH = g.Sum( t => t.QtyOnHand), SOH_Value = g.Aggregate((c,d) => decimal(c.QtyOnHand) * d.Price)};
但出现以下错误:
'System.Linq.IGrouping' does not contain a definition for 'Aggregate' and the best extension method overload 'System.Linq.Enumerable.Aggregate(System.Collections.Generic.IEnumerable, System.Func)' has some invalid arguments
这是我的实体定义:
public partial class ABCDashboard
{
public short TenantID { get; set; }
public string ABC { get; set; }
public double TotalSKU { get; set; }
public Nullable<double> SOH { get; set; }
public Nullable<decimal> SOH_Value { get; set; }
public Nullable<double> SOO { get; set; }
public Nullable<decimal> SOO_Value { get; set; }
}
在OMS_StockStatus中,QtyOnHand是双倍的?价格翻倍?
用 .. SOH_Value = g.Sum(t => ((decimal)t.QtyOnHand) * t.Price)