Luk*_*uke 12 c# sql linq asp.net
请帮我解决使用LINQ和GROUP和SUM的问题.
// Query the database
IEnumerable<BestSeller> best_sellers = from bs in (db.MYDATABASE).Take(25)
where bs.COMPANY == "MY COMPANY"
group bs by bs.PRODCODE into g
orderby g.Sum(g.MQTY)
select new BestSeller()
{
product_code = ,
product_description = ,
total_quantity =
};
Run Code Online (Sandbox Code Playgroud)
我希望:
BestSeller()对象我很困惑,因为只要我加入group混音,我的bs变量就变得无用了.
Jon*_*eet 31
我很困惑,因为只要我将我的组添加到混音中,我的bs变量就变得无用了.
是的,因为你不再有一个单一的项目-你现在处理的序列组项目.您可以获得每个组的第一项,我认为这是获取描述的有效方式?
var query = from bs in db.MYDATABASE.Take(25)
where bs.COMPANY == "MY COMPANY"
group bs by bs.PRODCODE into g
orderby g.Sum(x => x.MQTY)
select new BestSeller
{
product_code = g.Key,
product_description = g.First().DESCRIPTION,
total_quantity = g.Sum(x => x.MQTY)
};
Run Code Online (Sandbox Code Playgroud)
请注意,如果不指定顺序,"db.MYDATABASE中的前25项"没有意义."顶级"以什么方式?你可能想要:
from bs in db.MYDATABASE.OrderByDescending(x => x.Price).Take(25)
Run Code Online (Sandbox Code Playgroud)
或类似的东西.请注意,如果没有一家拥有"我公司"的公司,你最终都没有结果......
或者如果你想要前25名畅销书,你需要在最后的"拿"部分:
var query = from bs in db.MYDATABASE
where bs.COMPANY == "MY COMPANY"
group bs by bs.PRODCODE into g
orderby g.Sum(x => x.MQTY) descending
select new BestSeller
{
product_code = g.Key,
product_description = g.First().DESCRIPTION,
total_quantity = g.Sum(x => x.MQTY)
};
var top25 = query.Take(25);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40959 次 |
| 最近记录: |