1

另一个分配块!

基本上问题是我不能让我的输出按降序排列价格,同时保持按国家分组。

我知道这可能很简单,但我似乎无法理解。

有什么解决办法吗?

谢谢!

这是问题:“6. 允许用户按国家/地区降序查看销量前五的产品。(10 分)”

这是我的代码:

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

1 回答 1

0

“6. 允许用户按国家/地区降序查看销量前五的产品。(10分)”

我可以通过两种方式阅读。

A) 获取销量前五的产品,按国家/地区对这 5 种产品进行分组。或 B) 对于每个国家/地区,最畅销的五种产品是什么?

我认为B更有意义,所以我会做那个。

另外——什么是最畅销的产品?这个国家和它有什么关系?我认为客户的国家比供应商的国家更重要。另外 - 我认为 OrderDetails 中的 Quantity 可以告诉我哪些产品最畅销。注意:你的导师可能有其他想法,所以使用这些假设后果自负。

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()
}
于 2013-04-21T05:04:06.487 回答