2013-04-20 40 views
1

另一個分配塊!Linq to SQL - 按訂單發佈

基本上問題是,我不能讓我的輸出按降序排列價格,同時按國家分組。

我知道這可能很簡單,但我似乎無法得到它。

任何解決方案?

謝謝!

這裏有一個問題:「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; 
    } 

回答

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() 
} 
+0

在這個輸出端爲「System.Collections.Generic.List'1」 這對於摘要! – 2013-04-22 12:43:50