首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >orderby在C# LINQ内连接中的应用

orderby在C# LINQ内连接中的应用
EN

Stack Overflow用户
提问于 2014-05-29 11:09:40
回答 2查看 3.7K关注 0票数 3

我正在从C#试用此msdn页面的LINQ。

我无法在内部连接上申请订单,即使它适用于组连接。

运行查询的数据是:

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

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

代码语言:javascript
复制
Cola(Beverages)
Tea(Beverages)
Mustard(Condiments)
Pickles(Condiments)
Melons(Fruit)
Peaches(Fruit)
Bok Choy(Vegetables)
Carrots(Vegetables)

我能够使用group-joinorderby和第二个from-select生成这个输出,以变形组层次结构并生成简单的列表,如下所示:

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

但是,如果将orderby应用于内部联接(即没有into子句的group),则无法产生相同的输出。我尝试了以下变体:

变体#1

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

变体2

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

但是,这两个变体提供相同的输出,就好像没有使用orderby一样,并产生以下输出:

代码语言:javascript
复制
Cola (Beverages)
Tea (Beverages)
Mustard (Condiments)
Pickles (Condiments)
Carrots (Vegetables)
Bok Choy (Vegetables)
Peaches (Fruit)
Melons (Fruit)

如何使用inner-join orderby**?**生成所需的输出?

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

代码语言:javascript
复制
foreach (var product in simpleInnerJoin)
{
    Console.WriteLine(product.Product + " (" + product.Category + ")");
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-29 11:12:55

你的第二个选择应该很好。

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

输出:

代码语言:javascript
复制
[
    { 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" }
]

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

票数 6
EN

Stack Overflow用户

发布于 2014-05-29 11:14:58

将订单从主Linq中取出并使用

代码语言:javascript
复制
foreach (var product in simpleInnerJoin.Orderby(i=> i.Category.Name).ThenBy(i=>i.Product.Name))
{
    Console.WriteLine(product.Product + " (" + product.Category + ")");
}

我认为这应该给你你所需要的输出。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23931949

复制
相关文章

相似问题

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