如何通过使用C#Linq来完成“group by”来实现它[复制](How can “group by” be done using C# Linq to achieve it [duplicate]

编程入门 行业动态 更新时间:2024-10-28 02:27:11
如何通过使用C#Linq来完成“group by”来实现它[复制](How can “group by” be done using C# Linq to achieve it [duplicate])

这个问题在这里已经有了答案:

C#Linq Group By多列[复制] 2个答案

桌子设计:

Column1(Primary key) | Column2(Primary key) | Column3 | Column4 ---------------------------------------------------------------- 1 s 2 user1 1 d 3 user2 1 s 2 user1

在C#(linq)中,如何针对column1和column2执行“group by”操作,以便我无法获取重复行以及column3和column4数据?

例:

Column1(Primary key) | Column2(Primary key) | Column3 | Column4 ---------------------------------------------------------------- 1 s 2 user1 1 d 3 user2

This question already has an answer here:

C# Linq Group By on multiple columns [duplicate] 2 answers

Table design:

Column1(Primary key) | Column2(Primary key) | Column3 | Column4 ---------------------------------------------------------------- 1 s 2 user1 1 d 3 user2 1 s 2 user1

In C# (linq), how can 'group by' be done for column1 & column2 so that I can get no duplicate rows and also column3 & column4 data?

Example:

Column1(Primary key) | Column2(Primary key) | Column3 | Column4 ---------------------------------------------------------------- 1 s 2 user1 1 d 3 user2

最满意答案

正如Alejandro指出的那样,在主键上进行分组并不会带来任何好处,因为所有键的默认值都是唯一的,因此所有键的结果都是如此。

忽略它们是否是主键,可以使用类似于下面的内容在多个列上进行分组

from row in Table group row by new { row.ColumnA, row.ColumnB, ... ... } into gcs select new { ColumnA = gcs.Key.ColumnA, ColumnB = gcs.Key.ColumnB, ... ... };

As Alejandro has pointed out, grouping on primary keys won't give you any benefit since all keys are by default unique so all results as it as go in.

Ignoring if they are primary keys, you can use something like below to group by on multiple columns

from row in Table group row by new { row.ColumnA, row.ColumnB, ... ... } into gcs select new { ColumnA = gcs.Key.ColumnA, ColumnB = gcs.Key.ColumnB, ... ... };

更多推荐

本文发布于:2023-07-29 05:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1312607.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:来实现   来完成   Linq   group   duplicate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!