具有groupby,sum和average的实体框架lambda查询

时间:2011-12-21 09:42:29

标签: tsql entity-framework lambda group-by average

我有一个名为StockDetails的实体使用Entity Framework,见下图

我想要提取一个列表IEnumerable<StockDetail>,由Reels汇总,Qtyton,平均日期来自Days(日期时间),并按其余属性进行分组。< / p>

我正在使用Entity Framework构建一个数据层(WCF服务)作为ORM,一些服务是我试图转换为linq / lamdba表达式的旧SQL查询。但我对如何写作和想要一些帮助很新。

这是我在lambda中启动查询的方式,但是我被困在groupby / sum / average部分。

public IEnumerable<StockDetail> ListStockDetailByCustomerNumber(int customerNumber)
{
   var CustNo = customerNumber.ToString();

   return _entities.StockDetails
                .Where(x => x.Custno == CustNo)
                .GroupBy(
                    x =>
                    new
                        {
                            x.Millcd,
                            x.Matercd,
                            x.Proddesc,
                            x.Grammage,
                            x.Reelwidth,
                            x.Ordercode,
                            x.Buyordno,
                            x.Whsedesc,
                            x.Co,
                            x.Finished,
                            x.Pm,
                            x.PurchaseOrder,
                            x.Diameter,
                            x.Rtadate,
                            x.Custno,
                            x.Reels,
                            x.Days,
                            x.Qtyton
                        })
                .ToList();

        }

问题解决了:

public IEnumerable<StockDetail> ListStockDetailByCustomerNumber(int customerNumber)
        {
            var stockDetailsList = new List<StockDetail>();
            var custNo = customerNumber.ToString();

            var list = _entities.StockDetails
                       .Where(x => x.Custno == custNo )
                       .GroupBy(
                           x =>
                           new
                               {
                                   x.Millcd,
                                   x.Matercd,
                                   x.Proddesc,
                                   x.Grammage,
                                   x.Reelwidth,
                                   x.Ordercode,
                                   x.Buyordno,
                                   x.Whsedesc,
                                   x.Co,
                                   x.Finished,
                                   x.Pm,
                                   x.PurchaseOrder,
                                   x.Diameter,
                                   x.Rtadate,
                                   x.Custno,
                                   x.UpdDte
                               })
                       .Select(x => new
                                        {
                                            x.Key.Millcd,
                                            x.Key.Matercd,
                                            x.Key.Proddesc,
                                            x.Key.Grammage,
                                            x.Key.Reelwidth,
                                            x.Key.Ordercode,
                                            x.Key.Buyordno,
                                            Reels = x.Sum(p => p.Reels),
                                            Qtyton = x.Sum(p => p.Qtyton),
                                            Day = x.Max(p => p.Days),
                                            //Day = x.Average(p => p.Days.Ticks), // Want to calculate average datetime of date but linq dosn't support datetime.ticks
                                            x.Key.Whsedesc,
                                            x.Key.Co,
                                            x.Key.Finished,
                                            x.Key.Pm,
                                            x.Key.PurchaseOrder,
                                            x.Key.Diameter,
                                            x.Key.Rtadate,
                                            x.Key.Custno,
                                            x.Key.UpdDte
                                        });


            foreach (var s in list)
            {
                stockDetailsList.Add(new StockDetail
                                         {
                                             Millcd  = GetFriendlyNameForKey(s.Millcd),
                                             Matercd = s.Matercd,
                                             Proddesc = s.Proddesc,
                                             Grammage = s.Grammage,
                                             Reelwidth = s.Reelwidth,
                                             Ordercode = s.Ordercode,
                                             Buyordno = s.Buyordno,
                                             Reels = s.Reels,
                                             Qtyton = s.Qtyton,
                                             Days = s.Day,
                                             Whsedesc = s.Whsedesc,
                                             Co = s.Co,
                                             Finished = s.Finished,
                                             Pm = s.Pm,
                                             PurchaseOrder = s.PurchaseOrder,
                                             Diameter = s.Diameter,
                                             Rtadate = s.Rtadate,
                                             Custno = s.Custno,
                                             UpdDte = s.UpdDte
                                         });
            }

            return stockDetailsList;
        }

enter image description here

这是查询在T-SQL中的外观

SELECT 
   Millcd, Matercd,
   Proddesc, Grammage,
   Reelwidth, Ordercode,
   Buyordno,
   SUM(Reels) as Reels,
   SUM(Qtyton) as Qtyton,
   Whsedesc, Co, 
   (cast(FLOOR(avg(cast(DateProd as float))) as datetime)) As Days,
   Finished, Pm,
   PurchaseOrder,
   Diameter, Rtadate,
   Custno, UpdDte
FROM StockDetail
WHERE custno = @custcode
GROUP BY Millcd, Matercd, Proddesc, Grammage, Reelwidth, Ordercode, Buyordno, 
         Whsedesc, Co, Finished, Pm, PurchaseOrder, Diameter, Rtadate, Custno, UpdDte

1 个答案:

答案 0 :(得分:2)

不确定这是否会对您有所帮助,但您可以添加

  Reels = (_entities.StockDetails 
            .Where(x => x.Custno == CustNo).Sum(x=>x.Reels)) 
在您的选择中

而不是x.Reels,并对Qtyton

执行相同操作

平均使用平均延期

你的选择将在你的where语句之后看起来像.Select(x=>new {...}),然后是