LINQ 合并行中的结果
我有一个返回一组行的 LINQ 查询。结构是:
NAME, col1, col2, col3, col4
name1 1 null null null
name1 null 1 null null
name1 null null 1 1
因此,我想要一行包含
name1 1 1 1 1
所以我想按名称对这些结果进行分组并合并(求和?)其他列,因此如果我在一列中的某一行中没有 null - 我将收到除 null 之外的任何内容。
感谢您的帮助!
I have a LINQ query which returns a set of rows. The structure is:
NAME, col1, col2, col3, col4
name1 1 null null null
name1 null 1 null null
name1 null null 1 1
As a result I want to have one row containing
name1 1 1 1 1
So I want to group those results by name and merge (sum?) the other columns so if I have not null in one of the rows in a column - I will receive anything except null.
Thanks for help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
产量
和
产量
编辑:为了回应您的澄清...
我想您将能够从这里获取它。如果您想要做的只是找出组内是否有列,那么像 Bruno 的答案中那样的
Any
运算符就是正确的选择。仅当您尝试实际访问所有值以便执行更复杂的操作(例如对它们求和)时,才需要聚合(尽管正如 Jon 提到的,Sum 处理特定的值)案件)。简而言之,您想要的是像在两个答案中一样进行分组,然后在组中您可以使用
Aggregate
逐行合并,或者在结果上使用多个Any
GroupBy
取决于哪个在您的上下文中更清晰(或者如果每个组中有大量数据,则效率更高)yields
and
yields
EDIT: In response to your clarification...
I guess you'll be able to take it from here then. If all you want to do is find out whether there is a column within the group, then the
Any
operator like in Bruno's answer is the way to go.Aggregate
is only necessary if you're trying to actually visit all the values in order to do something more complex like summing them (although as Jon alluded to,Sum
handles that specific case).In short, what you want is grouping like in both the answer, and then within the group you either use
Aggregate
to merge row by row or multipleAny
on the results of theGroupBy
depending on which is clearer in your context (or more efficient if you have a large set of data within each group)