我有一个解决方案产品表,我想画出前10个最高销售额产品的图表。
用下面的代码尝试它,但我不知道确切的问题是什么,同时,它在逻辑上是正确的。
这是错误
System.ArgumentException:"DbExpressionBinding需要一个具有集合ResultType的输入表达式。参数名称:input‘
DateTime start = startDate.Value;
DateTime end = EndDate.Value;
var TopTen = (from q in db.soldProduct
where (q.addDate >= start && q.addDate <= end)
orderby q.Barcode.Count() descending
select new
{
count = q.Barcode.Count(),
q.productName
}).Take(10);
foreach (var item in TopTen)
{
chartCustomer.Series["Customer"].Points.AddXY(item.productName, item.count);
}
ADDED:-我在每一行都有项目名称和数量,可能是一个或多个,我也想把它添加到查询中,但越来越难了
解决方案清单的模型产品包含以下
public string barcode {get;set;}
public int productId{get;set;}
public string productName{get;set;}
public int quantity{get;set;}
public DateTime addDate{get;set;}
public double amount{get;set}
因此,如果该表具有以下数据
productId | 条形码 | productName数量 | >>添加日期 | >金额 |
---|---|---|---|---|
1 | 111 | 牛奶3 | 2020年3月2日 | 15 |
1 | 111 | 牛奶 | 1 | 2020年3月2日5 | //tr>
22 | 222 | 面包5 | 2020年3月2日 | 20 |
3 | 333 | 奶酪 | 1 | 2020年3月2日100 | //tr>
1 | 111 | 牛奶 | 3 | 2020年3月2日15 | //tr>
22 | 222 | 面包3 | 2020年3月2日15 |
注释后添加:
显然每个SoldProduct都有一个属性Amount。您不想计算SoldProducts的数量,而是要计算所有SoldProduct的总金额。
因此,如果产品"的SoldProduct1;牛奶;具有量10,并且对于产品"1"具有SoldProduct[22];牛奶;具有数量14,则作为产品"的结果;牛奶;已售出24件商品(=10+14件(
评论后结束添加
所以你有一张桌子,SoldProducts
。此表中的每一行代表一个产品的销售。
表SoldProducts至少有以下列的a.o.
AddDate
:销售是什么时候- CCD_ 3:销售产品的名称
Amount
:本次销售中售出的产品数量
我想要。。。销售额最高的前10种产品
"销售最高的产品";可能与售出产品的高度无关,而是与售出的产品总量有关。
要计算此值,您需要创建具有相同名称的SoldProducts组。您将获得名为"的soldProducts组;面包;以及名为";Milk";,等
然后,您需要求出每组所有soldProducts的总金额。
所以如果群";面包;有3个量为10、7、5的SoldProducts,那么你想要的结果是:["面包",22]。22是售出的面包总数。您可以按TotalAmount降序排列项目分组的结果,并取前10个项目。
根据一些常见的东西对元素进行分组是由Enumerable.GroupBy的一个重载完成的。在这种情况下,我使用参数resultSelector来计算每个组中的元素数量。
DateTime beginDate = ...
DateTime endDate = ...
var topTenSoldProductNames = dbContext.SoldProducts
.Where(soldProduct => beginDate <= soldProduct.AddDate
&& endDate >= soldProduct.AddDate)
// make groups with same ProductName
.GroupBy(soldProduct => soldProduct.ProductName,
// parameter resultSelector: take each ProductName,
// and all SoldProducts with this ProductName to make one new
(productName, soldProductsWithThisProductName) => new
{
Name = productName,
TotalAmount = soldProductsWithThisProductName
.Select(soldProduct => soldProduct.Amount)
.Sum(),
})
// order by descending TotalAmount, and take the first 10
.OrderByDescending(groupResult => groupResult.TotalAmount)
.Take(10);
您的查询是错误的,您返回的是字符数。如果不进行分组,此查询将毫无用处。
var groupingQuery =
from q in db.soldProduct
where (q.addDate >= start && q.addDate <= end)
group q by q.productName into g
select new
{
productName = g.Key,
count = g.Count(),
};
var TopTen = groupingQuery.OrderByDescending(x => x.count).Take(10);