C# 实体框架5方法查询汇总

C# 实体框架5方法查询汇总,c#,asp.net,entity-framework,asp.net-mvc-4,anonymous-types,C#,Asp.net,Entity Framework,Asp.net Mvc 4,Anonymous Types,我试图通过查询SaleConfirmation表来获得已确认/最终完成的购买的摘要,但是我在使用方法语法查询时遇到了很多困难 数据库表 以下是SaleConfirmation表结构,用于存储最终销售 Id OfferId ProdId Qty SaleDate ------------------------------------------------------- 10 7 121518 150 2013-03-14 00:00:

我试图通过查询SaleConfirmation表来获得已确认/最终完成的购买的摘要,但是我在使用方法语法查询时遇到了很多困难

数据库表
以下是SaleConfirmation表结构,用于存储最终销售

Id   OfferId   ProdId      Qty    SaleDate
-------------------------------------------------------
10   7         121518      150    2013-03-14 00:00:00.000
19   7         100518      35     2013-03-18 14:46:34.287
20   7         121518      805    2013-03-19 13:03:34.023
21   10        131541      10     2013-03-20 08:34:40.287
  • Id:唯一的行Id
  • OfferId:链接到要约/销售的外键 桌子
  • ProdId:products表中产品的ID
  • 数量:销售给客户的数量
  • SaleDate:完成销售的日期
控制器动作

var confRollUps = db.SaleConfirmation
                  .GroupBy(c => c.OfferId) // Ensure we get a list of unique/distinct offers
                  .Select(g => g.Select(i => new {
                            i.OfferId, 
                            i.Product.Variety, // "Category" of product, will be the same across products for this offer. i.Product is a SQL Server Navigation property.
                            i.Offer.Price, // The price of the product, set per offer. i.Offer is a SQL Server Navigation property.
                            i.Offer.Quantity, // The quantity of items that are expected to be sold before the offer expires
                            i.Offer.DateClose, // Date of when the offer expires
                            g.Sum(ii => ii.Qty) // Sum up the Qty column, we don't care about ProdIds not matching
                   }));
select查询中的错误为g.Sum(ii=>ii.Qty),错误如下

无效的匿名类型成员声明符。匿名类型成员必须 可以使用成员分配、简单名称或成员访问权限声明


您只需要将匿名类型分配给一个变量,试试这个

var confRollUps = db.SaleConfirmation
              .GroupBy(c => c.OfferId) // Ensure we get a list of unique/distinct offers
              .Select(g => g.Select(i => new {
                        OfferId = i.OfferId, 
                        ProductVariety = i.Product.Variety, // "Category" of product, will be the same across products for this offer. i.Product is a SQL Server Navigation property.
                        OfferPrice = i.Offer.Price, // The price of the product, set per offer. i.Offer is a SQL Server Navigation property.
                        OfferQty = i.Offer.Quantity, // The quantity of items that are expected to be sold before the offer expires
                        OfferDateClose =i.Offer.DateClose, // Date of when the offer expires
                        Total =g.Sum(ii => ii.Qty) // Sum up the Qty column, we don't care about ProdIds not matching
               }));