gpt4 book ai didi

c# - Entity Framework 5 方法查询汇总

转载 作者:太空狗 更新时间:2023-10-29 23:16:00 25 4
gpt4 key购买 nike

我试图通过查询 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:产品表中产品的 ID。
  • 数量:销售给客户的数量。
  • 销售日期:销售完成的日期。

Controller Action

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),错误如下。

Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

最佳答案

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

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
}));

关于c# - Entity Framework 5 方法查询汇总,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15533557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com