LINQ - 有没有办法在不使用 ForEach 的情况下在分组后获取元素值?

发布于 2024-12-08 20:57:35 字数 513 浏览 1 评论 0原文

我有一个包含 CategoryId 字段的 Items 表。这是类别表中的外键。我想按 CategoryId 对项目进行分组并返回 Items 和 Category.Name。

我知道我可以在 C# 中执行此操作:

var ItemsByCat = 
    from i in Items
    group i by i.CategoryId into g
    select g

foreach(var n in ItemsByCat)
{
    blah, blah
}

我的问题是 - 有没有一种方法可以获取分组的元素值部分,而无需使用循环进行迭代?比如:

from i in Items
group i by i.CategoryId into g
select new {
    CategoryID = g.Key,
    CategoryName = ???,
    Items = ???
}

非常感谢,

BK

I have an Items table that contains a CategoryId field. This is an FK into a Categories table. I want to group my Items by CategoryId and return the Items and Categories.Name.

I know I can do this in C#:

var ItemsByCat = 
    from i in Items
    group i by i.CategoryId into g
    select g

foreach(var n in ItemsByCat)
{
    blah, blah
}

My question is - is there a way to get at the element values part of the grouping without having to iterate through with a loop? Something like:

from i in Items
group i by i.CategoryId into g
select new {
    CategoryID = g.Key,
    CategoryName = ???,
    Items = ???
}

Many thanks,

BK

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我的鱼塘能养鲲 2024-12-15 20:57:35

我认为这就是您正在寻找的:

from i in Items
group i by i.CategoryId into g
select new 
{
    CategoryID = g.Key,
    CategoryName = g.First().Category.Name,
    Items = g
}

编辑:我希望这能从评论中回答您的问题。

g 是按 CategoryId 分组的项目集合。 select 中的 Items 可以是包含您需要的 Item 属性的任何子集的投影。例子:

from i in Items
group i by i.CategoryId into g
select new 
{
    CategoryID = g.Key,
    CategoryName = g.First().Category.Name,
    Items = g.Select(a => new{a.ItemID, a.ItemName})
}

I think this is what you're looking for:

from i in Items
group i by i.CategoryId into g
select new 
{
    CategoryID = g.Key,
    CategoryName = g.First().Category.Name,
    Items = g
}

EDIT: I hope this answers your question from the comments.

g is a collection of Items grouped by their CategoryId. Items in the select can be a projection including whatever subset of Item properties you require. Example:

from i in Items
group i by i.CategoryId into g
select new 
{
    CategoryID = g.Key,
    CategoryName = g.First().Category.Name,
    Items = g.Select(a => new{a.ItemID, a.ItemName})
}
简美 2024-12-15 20:57:35

加入类别表怎么样?

from i in Items
join c in Categories on i.CategoryId equals c.Id
group i by i.CategoryId into g
select new {
    CategoryID = g.Key,
    CategoryName = c.Name,
    Items = i
}

What about joining the Categories table?

from i in Items
join c in Categories on i.CategoryId equals c.Id
group i by i.CategoryId into g
select new {
    CategoryID = g.Key,
    CategoryName = c.Name,
    Items = i
}
千紇 2024-12-15 20:57:35

CategoryName 应为 i.Category.CategoryName //name 属性

如果您已正确设置关系,则

。这些项目应该是分组依据的值,不确定为什么您需要再次执行此操作。

CategoryName should be i.Category.CategoryName //the name property

given you have the relationship setup properly.

The items should be the values of group by, not sure why you'd need to do it again.

莫多说 2024-12-15 20:57:35

假设您在变量类别中有一个类别集合,它看起来像这样:

var ItemsByCategory = 
  from i in items
  join c in categories on i.CategoryId equals c.Id
  group i by i.CategoryId into g
  select new {
    CategoryId=g.Key,
    CategoryName=c.Name,
    Items=g.Items
}

Assuming you have a collection of Categories in a variable categories it would look like this:

var ItemsByCategory = 
  from i in items
  join c in categories on i.CategoryId equals c.Id
  group i by i.CategoryId into g
  select new {
    CategoryId=g.Key,
    CategoryName=c.Name,
    Items=g.Items
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文