如何通过使用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 user2This question already has an answer here:
C# Linq Group By on multiple columns [duplicate] 2 answersTable design:
Column1(Primary key) | Column2(Primary key) | Column3 | Column4 ---------------------------------------------------------------- 1 s 2 user1 1 d 3 user2 1 s 2 user1In 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, ... ... };更多推荐
发布评论