【问题标题】:Linq to SQL - Order by issueLinq to SQL - 按问题排序
【发布时间】:2013-04-20 12:16:36
【问题描述】:

另一个分配块!

基本上,问题是我无法让我的输出按降序排列价格,同时按国家/地区分组。

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

有什么解决办法吗?

谢谢!

这是一个问题: "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;
    }

【问题讨论】:

    标签: c# sql linq


    【解决方案1】:

    “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()
    }
    

    【讨论】:

    • 这里的输出最终为“System.Collections.Generic.List`1” 这是摘要!
    猜你喜欢
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多