gpt4 book ai didi

c# - 在简单/未命名的 C# LINQ group join 中订购内部键源

转载 作者:行者123 更新时间:2023-11-30 16:11:48 25 4
gpt4 key购买 nike

我知道这个问题由于使用了一些词而读起来很奇怪。但这是 this MSDN page 上使用的术语从我学习 LINQ group joins 的地方开始,我将对其进行解释。

我尝试使用 LINQ 的数据是:

    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},
};

现在是条款:

  • Simple/unnamed group join 是我们直接选择组的方式:

    //... 
    join product in products on category.ID equals product.CategoryID into prodGroup
    select prodGroup;

    而不是选择匿名类型或使用嵌套的 from-select 子句。

  • LINQ group join中的
  • Outer keysourcefrom关键字后的数据源,inner keysource后的数据源>加入关键字

    我将简单组连接称为未命名(为了方便起见),因为我们在所选组中没有外部键源名称/ID。

我正在尝试编写将产生以下结果的简单/未命名查询,首先按外部键源属性排序,然后按内部键源属性排序:

Unnamed Group
Cola
Tea
Unnamed Group
Mustard
Pickles
Unnamed Group
Melons
Peaches
Unnamed Group
Unnamed Group
Bok Choy
Carrots

我可以按如下方式按外部 key 源进行排序:

var simpleGroupJoinOrderby = 
from category in categories
orderby category.Name //orderby outer ks
join product in products on category.ID equals product.CategoryID into prodGroup
select prodGroup;

foreach (var unnamedGroup in simpleGroupJoinOrderby)
{
Console.WriteLine("Unnamed group");
foreach(var product in unnamedGroup)
{
Console.WriteLine(" " + product.Name);
}
}

但它产生以下输出:

Unnamed group
Cola
Tea
Unnamed group
Mustard
Pickles
Unnamed group
Peaches
Melons
Unnamed group
Unnamed group
Carrots
Bok Choy

但我无法在未命名类别组下订购产品。

我知道我可以通过选择匿名类型来做到这一点,如下所示:

        var namedGroupJoinOrderBy =
from category in categories
orderby category.Name //order group hierarchy by name
join product in products on category.ID equals product.CategoryID into prodGroup
select
new
{
Category = category.Name,
Products = from prod in prodGroup
orderby prod.Name //order by prodGroup.prod.Name under particular group hierarchy
select prod
};

foreach (var category in namedGroupJoinOrderBy)
{
Console.WriteLine("Category : " + category.Category);
foreach (var product in category.Products)
{
Console.WriteLine(" " + product.Name);
}
}

但是我只想知道我是否可以在不选择匿名类型的情况下做同样的事情。 我觉得它在语法上应该与“两个”键源的顺序和选择匿名类型无关。因此应该有一些方法可以在查询本身中执行此操作,如下所示,但它不起作用:

var simpleGroupJoinOrderby = 
from category in categories
orderby category.Name //orderby outer ks
join product in products on category.ID equals product.CategoryID into prodGroup
orderby product.Name //Err: The name 'product' does not exist in the current context
select prodGroup;

最佳答案

您可以在加入之前订购产品:

var simpleGroupJoinOrderby = 
from category in categories
orderby category.Name
join product in products.OrderBy(p => p.Name)
on category.ID equals product.CategoryID into prodGroup
select prodGroup;

结果是:

Unnamed group
Cola
Tea
Unnamed group
Mustard
Pickles
Unnamed group
Melons
Peaches
Unnamed group
Unnamed group
Bok Choy
Carrots

之所以有效,是因为 Join 保留了 outer 元素的顺序,并且对于这些元素中的每一个,保留了 inner 的匹配元素的顺序(引自 MSDN )。


一些替代方案(更像你的例子):

var simpleGroupJoinOrderby = categories.OrderBy (c => c.Name)
.GroupJoin(products,
c => c.ID,
p => p.CategoryID,
(c, ps) => ps.OrderBy(p => p.Name));

var simpleGroupJoinOrderby = from category in categories
orderby category.Name
join product in products
on category.ID equals product.CategoryID into prodGroup
select prodGroup.OrderBy(g => g.Name);

关于c# - 在简单/未命名的 C# LINQ group join 中订购内部键源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23952979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com