简体   繁体   English

将orderby应用于C#LINQ内连接

[英]Applying orderby to C# LINQ Inner join

I am trying out C# LINQ Joins from this msdn page . 我正在从这个msdn页面尝试C#LINQ Joins。

I am not able to apply order by on inner join even though it works with group join. 即使它适用于群组加入,我也无法通过内部联接应用订单。

The data on which queries are run are: 运行查询的数据是:

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

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

    // Specify the first data source.
    static List<Category> categories = new List<Category>()
    { 
        new Category(){Name="Beverages", ID=001},
        new Category(){ Name="Condiments", ID=002},
        new Category(){ Name="Vegetables", ID=003},
        new Category() {  Name="Grains", ID=004},
        new Category() {  Name="Fruit", ID=005}            
    };

    // Specify the second data source.
    static List<Product> products = new List<Product>()
    {
        new Product{Name="Cola",  CategoryID=001},
        new Product{Name="Tea",  CategoryID=001},
        new Product{Name="Mustard", CategoryID=002},
        new Product{Name="Pickles", CategoryID=002},
        new Product{Name="Carrots", CategoryID=003},
        new Product{Name="Bok Choy", CategoryID=003},
        new Product{Name="Peaches", CategoryID=005},
        new Product{Name="Melons", CategoryID=005},
    };

The desired output is (list order by categories in brackets and then by product) : 所需的输出是 (按括号中的类别列出顺序,然后按产品排序):

Cola(Beverages)
Tea(Beverages)
Mustard(Condiments)
Pickles(Condiments)
Melons(Fruit)
Peaches(Fruit)
Bok Choy(Vegetables)
Carrots(Vegetables)

I was able to produce this output using group-join with orderby and 2nd from-select to deform group hierarchy and produce plain list as follows: 我能够使用orderby group-joinorderby以及第二个from-select来生成此输出,以使组层次结构变形并生成如下的简单列表:

var listGroupJoinOrderBy =
            from category in categories
            join product in products on category.ID equals product.CategoryID into prodGroup
            from prod in prodGroup
            orderby category.Name, prod.Name  //first order by category name then product name
            select
            new
            {
                Category = category.Name,
                Product = prod.Name
            };

But then I am unable to produce this same output with orderby applied to inner join (that is group-join without into clause). 但是后来我无法使用orderby应用于内连接(即没有into子句的group-join)生成相同的输出。 I tried following variants: 我试过以下变种:

Variant #1 变体#1

var innerJoinOrderBy =
            from category in categories
            orderby category.Name    //orderby category name
            join product in products on category.ID equals product.CategoryID                
            orderby product.Name     //orderby product name
            select
            new
            {
                Category = category.Name,
                Product = product.Name
            };

Variant #2 变种#2

var innerJoinOrderBy =
            from category in categories

            join product in products on category.ID equals product.CategoryID                
            orderby category.Name, product.Name     //orderby category first and then by product name
            select
            new
            {
                Category = category.Name,
                Product = product.Name
            };    

However both variants give the same output as if no orderby is used, and produces the following output: 但是,两个变体都提供相同的输出,就像没有使用orderby ,并产生以下输出:

Cola (Beverages)
Tea (Beverages)
Mustard (Condiments)
Pickles (Condiments)
Carrots (Vegetables)
Bok Choy (Vegetables)
Peaches (Fruit)
Melons (Fruit)

Q. How can I produce the desired output with inner-join and orderby ? 问:如何使用inner-joinorderby生成所需的输出?

Anyway, to print the query result one can use following foreach (just change the query variable name) : 无论如何,要打印查询结果,可以使用以下foreach (只需更改查询变量名称):

foreach (var product in simpleInnerJoin)
{
    Console.WriteLine(product.Product + " (" + product.Category + ")");
}

Your second option should work fine 你的第二个选择应该工作正常

var innerJoinOrderBy =
            from c in categories
            join p in products on c.ID equals p.CategoryID
            orderby c.Name, p.Name
            select new {
                Category = c.Name,
                Product = p.Name
            };

Output: 输出:

[
    { Product="Cola", Category="Beverages" },
    { Product="Tea", Category="Beverages" },
    { Product="Mustard", Category="Condiments" },
    { Product="Pickles", Category="Condiments" },
    { Product="Melons", Category="Fruit" },
    { Product="Peaches", Category="Fruit" },
    { Product="Bok Choy", Category="Vegetables" },
    { Product="Carrots", Category="Vegetables" }
]

From your output I see that items go in their original order. 从您的输出中我看到项目按原始顺序排列。 Make sure you have applied orderby operator in query. 确保在查询中应用了orderby运算符。

Take the Orderby out of the main Linq and use 将Orderby从主Linq中取出并使用

foreach (var product in simpleInnerJoin.Orderby(i=> i.Category.Name).ThenBy(i=>i.Product.Name))
{
    Console.WriteLine(product.Product + " (" + product.Category + ")");
}

I think this should give you the output you require. 我认为这应该为您提供所需的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM