使用 select 语句对多级对象进行 Linq Group
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
附加评论作为结束问题的答案:-
当然,如果对结果进行分组,则无法选择子项的一列,那是因为可能有多个子项,并且您必须指定一个聚合列,例如 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 –