Linq 按产品排序但显示类别

Linq sort by products but display cateogires

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

排序

但是问题是我还是想select一个分类,可是我怎么办 首先按 product.name 排序,而不添加额外的连接或 select.

来自类别中的类别 select category.name orderby category.Name //orderby 类别名称

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

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

class 产品 { public 字符串名称 { 得到;放; } public int CategoryID { 得到;放; } }

class 类别 { public 字符串名称 { 得到;放; } public int ID { 得到;放; } }

// 指定第一个数据源。 静态列表类别=新列表() { 新类别(){名称="Beverages", ID=001}, 新类别(){ 名称="Condiments", ID=002}, 新类别(){ 名称="Vegetables", ID=003}, 新类别() { 名称="Grains", ID=004}, 新类别() { 名称="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}, };

哦,我看到你查询了,格式不好。

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

//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);
            }
        }



} 
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);
            }
        }



}