C# Linq到SQL-按问题排序
另一个任务块 基本上,问题是我不能让我的产出以降序排列价格,同时保持按国家分组 我知道这可能很简单,但我就是不明白 有什么解决办法吗 谢谢 问题是: “6.允许用户按国家/地区降序查看前五大销售产品。 (10马克) 这是我的密码:C# Linq到SQL-按问题排序,c#,sql,linq,C#,Sql,Linq,另一个任务块 基本上,问题是我不能让我的产出以降序排列价格,同时保持按国家分组 我知道这可能很简单,但我就是不明白 有什么解决办法吗 谢谢 问题是: “6.允许用户按国家/地区降序查看前五大销售产品。 (10马克) 这是我的密码: void MainWindow_Loaded(object sender, RoutedEventArgs e) { var q6 = (from t in northwind.Products orderby
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
var q6 = (from t in northwind.Products
orderby t.UnitPrice descending
join o in northwind.Suppliers on t.SupplierID equals o.SupplierID
group t.UnitPrice by new {o.Country, t.UnitPrice} into grouped
select new
{
Output = grouped.Key
}).Take(5);
lbxTop5.ItemsSource = q6;
}
“6.允许用户按国家/地区按降序分组查看前五名销售产品。(10分)”
我可以从两个方面来理解
A) 获得最畅销的五种产品,按国家对这五种产品进行分组。
或
B) 对于每个国家,最畅销的五种产品是什么
我认为B更有意义,所以我就做那个
还有-什么是最畅销的产品?这个国家和它有什么关系?我认为客户的国家比供应商的国家更重要。此外,我认为OrderDetails中的数量可以告诉我哪些产品最畅销。注意:你的指导老师可能有我以外的其他想法,因此使用这些假设的风险自负
from c in northwind.Customers
from o in c.Orders //all froms except first are calls to SelectMany (one to many)
from od in o.OrderDetails //navigational properties -> no need to write explicit joins
let p = od.Product // now we go many to one, so we don't need SelectMany
group od
by new {c.Country, Product = p } //anon grouping key
into productGroup
let country = productGroup.Key.Country
let product = productGroup.Key.Product
let quantity = productGroup.Sum(od2 => od2.Quantity)
group new {Product = product, Quantity = quantity} //anon group elements
by country
into countryGroup
select new {
Country = countryGroup.Key,
Summaries = countryGroup
.OrderByDescending(summary => summary.Quantity)
.ThenBy(summary => summary.Product.ProductId) //tiebreaker
.Take(5)
.ToList()
}
这里的输出结果是“System.Collections.Generic.List`1”,用于摘要!