在 C# 中按 2 列和 Max() 其他列分组
Group By 2 column and Max() others in C#
什么方法让我能够按 2 列分组并得到 MAX
彼此的列
假设我有这个:
DataTable dt = new DataTable();
dt.Columns.Add("s1", typeof(string));
dt.Columns.Add("s2", typeof(string));
dt.Columns.Add("nt1", typeof(int));
dt.Columns.Add("nt2", typeof(int));
// Here we add five DataRows.
dt.Rows.Add("g1", "gg1", 1, 16);
dt.Rows.Add("g2", "gg1", 2, 15);
dt.Rows.Add("g1", "gg1", 3, 14);
dt.Rows.Add("g2", "gg1", 4, 13);
dt.Rows.Add("g1", "gg2", 5, 12);
dt.Rows.Add("g2", "gg2", 6, 11);
dt.Rows.Add("g1", "gg2", 7, 10);
dt.Rows.Add("g2", "gg2", 8, 9);
我想得到的是:
g1 gg1 3 16
g1 gg2 7 12
g2 gg1 4 15
g2 gg2 8 11
此代码无效:
dt = dt.AsEnumerable()
.GroupBy(r => new
{
s1 = r["s1"],
s2 = r["s2"]
})
.Select(x => x.Max())
.CopyToDataTable();
给我错误:
Severity Code Description Project File Line Suppression State Error
CS0311 The type 'System.Linq.IGrouping<string, string>' cannot be used
as type parameter 'T' in the generic type or method
'DataTableExtensions.CopyToDataTable(IEnumerable)'. There is no
implicit reference conversion from 'System.Linq.IGrouping<string,
string>' to 'System.Data.DataRow'. testt
C:\Users\TECNO\source\repos\testt\testt\Program.cs 44 Active
因此,由于类型不匹配,错误发生在 CopyToDataTable
上。您需要将 GroupBy
的结果转换为 DataRow
的 IEnumerable
。
我很长时间没有使用 LINQ,你需要在调用 CopyToDataTable
之前应用 Select
并确保你 select s1, s2,最大值 (nt1) 和最大值 (nt2)。也许你还需要调用ToList
,我不确定,但你肯定需要在分组后再调用一次Select
。
好的,此代码可以解决问题
谢谢大家
var query = dt.AsEnumerable()
.GroupBy(x => new
{
s1 = x["s1"],
s2 = x["s2"]
})
.Select(grp => new {
s1 = grp.Key.s1,
s2 = grp.Key.s2,
nt1 = grp.Max(x => x["nt1"]),
nt2 = grp.Max(x => x["nt2"])
});
DataTable dt2 = dt.Clone();
dt2.Clear();
foreach (var item in query)
dt2.Rows.Add(item.s1, item.s2, item.nt1, item.nt2);
什么方法让我能够按 2 列分组并得到 MAX
彼此的列
假设我有这个:
DataTable dt = new DataTable();
dt.Columns.Add("s1", typeof(string));
dt.Columns.Add("s2", typeof(string));
dt.Columns.Add("nt1", typeof(int));
dt.Columns.Add("nt2", typeof(int));
// Here we add five DataRows.
dt.Rows.Add("g1", "gg1", 1, 16);
dt.Rows.Add("g2", "gg1", 2, 15);
dt.Rows.Add("g1", "gg1", 3, 14);
dt.Rows.Add("g2", "gg1", 4, 13);
dt.Rows.Add("g1", "gg2", 5, 12);
dt.Rows.Add("g2", "gg2", 6, 11);
dt.Rows.Add("g1", "gg2", 7, 10);
dt.Rows.Add("g2", "gg2", 8, 9);
我想得到的是:
g1 gg1 3 16
g1 gg2 7 12
g2 gg1 4 15
g2 gg2 8 11
此代码无效:
dt = dt.AsEnumerable()
.GroupBy(r => new
{
s1 = r["s1"],
s2 = r["s2"]
})
.Select(x => x.Max())
.CopyToDataTable();
给我错误:
Severity Code Description Project File Line Suppression State Error CS0311 The type 'System.Linq.IGrouping<string, string>' cannot be used as type parameter 'T' in the generic type or method 'DataTableExtensions.CopyToDataTable(IEnumerable)'. There is no implicit reference conversion from 'System.Linq.IGrouping<string, string>' to 'System.Data.DataRow'. testt C:\Users\TECNO\source\repos\testt\testt\Program.cs 44 Active
因此,由于类型不匹配,错误发生在 CopyToDataTable
上。您需要将 GroupBy
的结果转换为 DataRow
的 IEnumerable
。
我很长时间没有使用 LINQ,你需要在调用 CopyToDataTable
之前应用 Select
并确保你 select s1, s2,最大值 (nt1) 和最大值 (nt2)。也许你还需要调用ToList
,我不确定,但你肯定需要在分组后再调用一次Select
。
好的,此代码可以解决问题
谢谢大家
var query = dt.AsEnumerable()
.GroupBy(x => new
{
s1 = x["s1"],
s2 = x["s2"]
})
.Select(grp => new {
s1 = grp.Key.s1,
s2 = grp.Key.s2,
nt1 = grp.Max(x => x["nt1"]),
nt2 = grp.Max(x => x["nt2"])
});
DataTable dt2 = dt.Clone();
dt2.Clear();
foreach (var item in query)
dt2.Rows.Add(item.s1, item.s2, item.nt1, item.nt2);