2015-09-11 36 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