2015-09-11 37 views
0

所以我有一個問題,代碼是選擇一個類別 ,我需要改變排序順序按product.Name然後按category.name。Linq按產品排序,但顯示cigogire

但問題是我仍然想選擇一個類別,但是如何在不添加額外連接或選擇的情況下先按product.name排序 。

從類別中的類別選擇 category.name 的OrderBy category.Name //排序依據類別名稱

後來在視圖我環的foreach(category.products),並通過在category.product [I],以查看以顯示

但排序順序是錯誤的,順序總是由Category.Name 如何按Product.Name第一次,然後通過Category.Name? 請問SelectMany有幫助嗎?再次,我不想破壞我的查詢中的選擇 部分,只是按東西排序。

class產品 { public string Name {get;組; } public int CategoryID {get;組; } }

class Category { public string Name {get;組; } public int ID {get;組; } }

//指定第一個數據源。 靜態列表類別=新列表() { 新分類(){名稱= 「飲料」,ID = 001}, 新分類(){名稱= 「調味品」,ID = 002}, 新的類別() {Name =「Vegetables」,ID = 003} 新類別(){Name =「Grains」,ID = 004},

//指定第二個數據源。新產品{Name =「Cola」,CategoryID = 001}, 新產品{Name =「Tea」,CategoryID = 001}, 新產品{Name =「Mustard」 ,CategoryID = 002}, 新產品{Name =「pickles」,CategoryID = 002}, 新產品{Name =「Carrots」,CategoryID = 003}, 新產品{Name =「Bok Choy」,CategoryID = 003 }, 新產品{Name =「Peaches」,CategoryID = 005}, 新產品{Name =「Melons」,CategoryID = 005}, };

+0

你可以告訴你正在使用的查詢? –

+0

抱歉,我無法發表評論,但我犯了一個錯誤 – user114141

+0

class class { – user114141

回答

0

哦,我看到你的查詢,它格式不正確。

您需要order by Product.Name group by Category.Name

+0

看我上面格式化的代碼 – user114141

0
//LinqPad code 

class Category 
{ 
    public string Name { get; set; } 
    public int ID { get; set; } 
    public List<Product> products { get; set;} 
} 

class Product 
{ 
    public string Name { get; set; } 
    public int ID { get; set; } 
} 


void Main() 
{ 
     // Specify the second data source. 
     List<Product> BevProducts = new List<Product>() 
     { 
      new Product{Name="ZCola"}, 
     }; 

     // Specify the third data source. 
     List<Product> CondProducts = new List<Product>() 
     { 
      new Product{Name="Sugar"}, 
     }; 
     // Specify the first data source. 
    List<Category> categories = new List<Category>() 
     { 
      new Category(){Name="Beverages", ID=001, products=BevProducts}, 
      new Category(){ Name="Condiments", ID=002, products=CondProducts}, 

     }; 



     var sortedCats = categories.OrderBy(c => c.ID).ToList(); 

     foreach (var category in sortedCats) 
     { 
      //display category 
      System.Console.Out.WriteLine(category.Name); 

     //Assuming each category contains exactly one product in the list ie 1 to 1 relationship 
// how can I sort by product.Name, so if ZCola comes before Sugar, ZCola's Category (Beverages) sorts before Condiments 
// so in this Case Beverages, Condiments is the right order, because ZCola comes after Sugar. 
      var sortedProductsPerCategory = category.products.OrderBy(p => p.Name).ToList(); 

      foreach (var product in sortedProductsPerCategory) 
      { 
        //display product 
        System.Console.Out.WriteLine(" " + product.Name); 
      } 
     } 



} 
+0

所以右邊的命令是Sugar,ZCola但是這個打印ZCola,Sugar – user114141

0
class Category 
{ 
    public string Name { get; set; } 
    public int ID { get; set; } 
    public List<Product> products { get; set;} 
} 

class Product 
{ 
    public string Name { get; set; } 
    public int ID { get; set; } 
} 


void Main() 
{ 
     // Specify the second data source. 
     List<Product> BevProducts = new List<Product>() 
     { 
      new Product{Name="ZCola"}, 
     }; 

     // Specify the third data source. 
     List<Product> CondProducts = new List<Product>() 
     { 
      new Product{Name="Sugar"}, 
     }; 
     // Specify the first data source. 
    List<Category> categories = new List<Category>() 
     { 
      new Category(){Name="Beverages", ID=001, products=BevProducts}, 
      new Category(){ Name="Condiments", ID=002, products=CondProducts}, 

     }; 



     var sortedCats = categories.OrderBy(c => c.products.Min(p => p.Name)).ToList(); 

     foreach (var category in sortedCats) 
     { 
      //display category 
      System.Console.Out.WriteLine(category.Name); 


      //Assuming each category contains exactly one product in the list ie 1 to 1 relationship 
      // how can I sort by product.Name, so if ZCola comes before Sugar, ZCola's Category (Beverages) sorts before Condiments 
      // so in this Case Beverages, Condiments is the right order, because ZCola comes after Sugar. 
      var sortedProductsPerCategory = category.products.OrderBy(p => p.Name).ToList(); 

      foreach (var product in sortedProductsPerCategory) 
      { 
        //display product 
        System.Console.Out.WriteLine(" " + product.Name); 
      } 
     } 



} 
+0

這是正確答案..需要使用敏..抱歉的混亂! – user114141