【问题标题】:Linq sort by products but display cateogiresLinq 按产品排序但显示类别
【发布时间】:2015-12-08 13:46:27
【问题描述】:

所以我遇到了代码选择类别的问题 我需要更改排序顺序以按 product.Name 然后按 category.name 排序。

但问题是我还是想选择一个类别,但是我该怎么做 首先按 product.name 排序,无需添加额外的连接或选择。

从类别中的类别 选择类别名称 orderby category.Name //orderby类别名称

稍后在视图中我循环 foreach(category.products) 并传入 category.product[i] 以查看以显示

但是排序顺序是错误的,顺序总是按 Category.Name 如何先按 Product.Name 排序,然后按 Category.Name 排序? SelectMany 会有帮助吗?同样,我不想破坏选择 我的查询的一部分,只是 Order by stuff。

类产品 { 公共字符串名称 { 获取;放; } 公共 int 类别 ID { 获取;放; } }

类类别 { 公共字符串名称 { 获取;放; } 公共 int ID { 获取;放; } }

// 指定第一个数据源。 静态列表类别 = 新列表() { 新类别(){Name="Beverages", ID=001}, new Category(){ Name="Condiments", ID=002}, 新类别(){名称=“蔬菜”,ID=003}, 新类别(){名称=“谷物”,ID=004}, 新类别() { Name="Fruit", ID=005}
};

// 指定第二个数据源。 静态列表产品 = 新列表() { 新产品{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}, };

【问题讨论】:

  • 对不起,我不能发表评论,但我犯了一个错误
  • class Category { 列出 产品;公共字符串名称 { 获取;放; } 公共 int ID { 获取;放; } }
  • 对不起,我应该说类别有一个列表或产品。也许是类中的一个属性。
  • 这里是修改后的代码,你可以抽入 LInqPad4

标签: linq list sorting product


【解决方案1】:

哦,我看到你查询了,格式错误。

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

【讨论】:

    【解决方案2】:
    //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);
                }
            }
    
    
    
    } 
    

    【讨论】:

    • 所以正确的顺序是 Sugar, ZCola 但这会打印 ZCola, Sugar
    【解决方案3】:
    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);
                }
            }
    
    
    
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-13
      • 1970-01-01
      • 2015-12-09
      • 2015-06-25
      • 1970-01-01
      相关资源
      最近更新 更多