使用 select 语句对多级对象进行 Linq Group

发布于 2024-08-30 19:36:16 字数 558 浏览 10 评论 0原文

我有 3 个数据集对象,它们使用实体集对象相互嵌套。我正在选择这样的数据

var newList = from s in MainTable
from a in s.SubTable1 where a.ColumnX = "value"
from b in a.Detail where b.Name = "searchValue"
select new {
    ID = s.ID,
    Company = a.CompanyName,
    Name = b.Name,
    Date = s.DueDate
    Colour = b.Colour,
    Town = a.Town
};

,这工作正常,但问题是每个名称值的详细信息对象列表/表中都有许多记录,因此我会得到大量重复行,因此我只想为每个名称显示一条记录b. 姓名。我尝试将其放在

group s by b.Name into g

选择之前,但这似乎会停止选择,使我能够选择我想要的列(实际上还有更多列)。在这种情况下如何使用 group 命令,同时仍保持输出行为“平面”格式?

I've got 3 dataset objects that are nested with each other using entity set objects. I am selecting the data like this

var newList = from s in MainTable
from a in s.SubTable1 where a.ColumnX = "value"
from b in a.Detail where b.Name = "searchValue"
select new {
    ID = s.ID,
    Company = a.CompanyName,
    Name = b.Name,
    Date = s.DueDate
    Colour = b.Colour,
    Town = a.Town
};

and this works fine, but the trouble is there are many records in the Detail object-list/table for each Name value so I get a load of duplicate rows and thus I only want to display one record per b.Name. I have tried putting

group s by b.Name into g

before the select, but then this seems to stop the select enabling me to select the columns I want (there are more, in practice). How do I use the group command in this circumstance while still keeping the output rows in a "flat" format?

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

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

发布评论

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

评论(1

紫﹏色ふ单纯 2024-09-06 19:36:16

附加评论作为结束问题的答案:-
当然,如果对结果进行分组,则无法选择子项的一列,那是因为可能有多个子项,并且您必须指定一个聚合列,例如 sum,max etx –

Appending comment as answer to close question:-
Of course that if you group your results, you cant get select a column of a child, thats because there may be more than one childs and you have to specify an aggregate column for example the sum,max etx –

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文