在c# LINQ内连接中应用顺序
本文关键字:应用 顺序 连接 LINQ | 更新日期: 2023-09-27 18:05:47
我正在从msdn页面尝试c# LINQ join。
我不能应用order by on内连接,即使它与组连接工作。
执行查询的数据有:
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},
};
期望的输出是(先按括号内的类别排序,然后按产品排序):
Cola(Beverages)
Tea(Beverages)
Mustard(Condiments)
Pickles(Condiments)
Melons(Fruit)
Peaches(Fruit)
Bok Choy(Vegetables)
Carrots(Vegetables)
我能够使用group-join
与orderby
和第二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
};
但是,我无法与应用于内连接的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
};
变体# 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
};
然而,这两个变量给出的输出与没有使用orderby
时相同,并产生以下输出:
Cola (Beverages)
Tea (Beverages)
Mustard (Condiments)
Pickles (Condiments)
Carrots (Vegetables)
Bok Choy (Vegetables)
Peaches (Fruit)
Melons (Fruit)
Q。如何使用inner-join
和orderby
生成所需的输出?
无论如何,要打印查询结果,可以使用以下foreach
(只需更改查询变量名称):
foreach (var product in simpleInnerJoin)
{
Console.WriteLine(product.Product + " (" + product.Category + ")");
}
第二个选项应该没问题
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
};
输出:[
{ 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
运算符
将order从主Linq中取出并使用
foreach (var product in simpleInnerJoin.Orderby(i=> i.Category.Name).ThenBy(i=>i.Product.Name))
{
Console.WriteLine(product.Product + " (" + product.Category + ")");
}