首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Linq按产品分类,但显示猫猫

Linq按产品分类,但显示猫猫
EN

Stack Overflow用户
提问于 2015-09-11 22:20:24
回答 3查看 188关注 0票数 0

因此,我遇到了一个问题,代码选择了一个类别,我需要更改排序顺序,以便按product.Name排序,然后按category.name排序。

但问题是,我仍然希望选择一个类别,但是如何在不添加额外的联接或选择的情况下,首先按照product.name进行排序。

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

稍后,在视图i循环预见(category.products)和传入category.producti以查看显示

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

类产品{公共字符串名{ get;set;} public int CategoryID { get;set;}

类类别{公共字符串名{ get;set;} public int ID { get;set;}

//指定第一个数据源。静态列表类别=新列表(){新类别(){Name=“饮料”,ID=001},新类别(){Name=“调味品”,ID=002},新类别(){Name=“蔬菜”,ID=003},新类别(){Name=“谷物”,ID=004},新类别(Name=“水果”,ID=005} )

};

//指定第二个数据源。静态列表产品=新列表(){新产品{Name=“可乐”、CategoryID=001}、新产品{Name=“茶”、CategoryID=001}、新产品{Name=“芥末”、CategoryID=002}、新产品{Name=“泡菜”、CategoryID=002}、新产品{Name=“胡萝卜”、CategoryID=003}、新产品{Name=“Bok Choy”、CategoryID=003}、新产品{Name=“桃”、CategoryID=005}、新产品{Name=“甜瓜”、CategoryID=005}、};

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-09-11 23:17:42

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2015-09-11 22:40:25

哦,我看到你的查询,格式很差。

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

票数 0
EN

Stack Overflow用户

发布于 2015-09-11 23:06:26

代码语言:javascript
复制
//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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32532937

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档