将 orderby 应用于 C# LINQ 内连接

Mah*_*aha 3 c# linq join inner-join

我正在从这个 msdn 页面尝试 C# LINQ Joins 。

即使它与组连接一起使用,我也无法在内部连接上应用 order by。

运行查询的数据是:

    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},
    };
Run Code Online (Sandbox Code Playgroud)

所需的输出是(按括号中的类别然后按产品列出顺序):

Cola(Beverages)
Tea(Beverages)
Mustard(Condiments)
Pickles(Condiments)
Melons(Fruit)
Peaches(Fruit)
Bok Choy(Vegetables)
Carrots(Vegetables)
Run Code Online (Sandbox Code Playgroud)

我能够使用group-joinwithorderby和 2nd生成此输出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
            };
Run Code Online (Sandbox Code Playgroud)

但是后来我无法使用orderby应用于内部连接(即没有into子句的组连接)产生相同的输出。我尝试了以下变体:

变体#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
            };
Run Code Online (Sandbox Code Playgroud)

变体#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
            };    
Run Code Online (Sandbox Code Playgroud)

然而,两种变体都提供与orderby使用no 相同的输出,并产生以下输出:

Cola (Beverages)
Tea (Beverages)
Mustard (Condiments)
Pickles (Condiments)
Carrots (Vegetables)
Bok Choy (Vegetables)
Peaches (Fruit)
Melons (Fruit)
Run Code Online (Sandbox Code Playgroud)

问:如何使用inner-join和生成所需的输出orderby

无论如何,要打印查询结果可以使用以下foreach(只需更改查询变量名称):

foreach (var product in simpleInnerJoin)
{
    Console.WriteLine(product.Product + " (" + product.Category + ")");
}
Run Code Online (Sandbox Code Playgroud)

Ser*_*kiy 5

您的第二个选项应该可以正常工作

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
            };
Run Code Online (Sandbox Code Playgroud)

输出:

[
    { 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" }
]
Run Code Online (Sandbox Code Playgroud)

从您的输出中,我看到项目按其原始顺序排列。确保您orderby在查询中应用了运算符。