Linq Group和Aggregate

时间:2015-01-29 11:52:29

标签: c# linq entity-framework-5

我正在尝试为以下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'不包含' Aggregate'的定义和最好的扩展方法重载' System.Linq.Enumerable.Aggregate(System.Collections.Generic.IEnumerable,System.Func)'有一些无效的论点


这是我的实体定义:

    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是双倍的吗?而价格是双倍?

1 个答案:

答案 0 :(得分:1)

尝试使用.. SOH_Value = g.Sum(t =&gt;((decimal)t.QtyOnHand)* t.Price)