简单的 LINQ 数据表聚合不起作用
Simple LINQ DataTable Aggregation Doesn't Work
所以我有这样一个数据集:
Group Value
A 1
A 2
B 2
B 5
简直我只想拥有:
Group Value
A 3
B 7
作为另一个 DataTable 变量。这是我的 C# 代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.OleDb;
using System.Globalization;
using System.IO;
using utilityClass;
namespace testData
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
string path = "test_data.csv";
bool isFirstRowHeader = true;
dt = Utils.GetDataTableFromCsv(path, isFirstRowHeader);
var results = from row in dt.AsEnumerable()
group row by row.Field<string>("Group") into grp
select new
{
Id = grp.Key,
sum = grp.Sum(r => r.Field<int>("Value"))
};
DataTable newDataTbl = results.CopyToDataTable();
}
}
}
它在 CopyToDataTable() 中给我错误说:
有人知道我的代码出了什么问题吗?谢谢!
您的结果不是 DataRows
的序列。 CopyToDataTable()
仅在 IEnumerable<T>
上可用,其中 T
是或源自 DataRow
(更多信息在此 link 中)。
正如您在引用的 link 中看到的那样,有一个解决方案。为此,您将需要实施基于反射的解决方法:
How to: Implement CopyToDataTable Where the Generic Type T Is Not a DataRow
您可以通过使用 where 语句分配数据行对象的 ItemArray 并检查它是否为空来在 from
语句中创建数据行集合。像这样:
static void Main(string[] args)
{
DataTable dt = new DataTable();
string path = "test_data.csv";
bool isFirstRowHeader = true;
dt = Utils.GetDataTableFromCsv(path, isFirstRowHeader);
var results = from row in dt.AsEnumerable()
group row by row.Field<string>("Group") into grp
let dr = dt.NewRow()
where (dr.ItemArray = new object[] { grp.Key, grp.Sum(r => r.Field<int>("Value")) }) != null
select dr ;
DataTable newDataTbl = results.CopyToDataTable();
}
所以我有这样一个数据集:
Group Value
A 1
A 2
B 2
B 5
简直我只想拥有:
Group Value
A 3
B 7
作为另一个 DataTable 变量。这是我的 C# 代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.OleDb;
using System.Globalization;
using System.IO;
using utilityClass;
namespace testData
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
string path = "test_data.csv";
bool isFirstRowHeader = true;
dt = Utils.GetDataTableFromCsv(path, isFirstRowHeader);
var results = from row in dt.AsEnumerable()
group row by row.Field<string>("Group") into grp
select new
{
Id = grp.Key,
sum = grp.Sum(r => r.Field<int>("Value"))
};
DataTable newDataTbl = results.CopyToDataTable();
}
}
}
它在 CopyToDataTable() 中给我错误说:
有人知道我的代码出了什么问题吗?谢谢!
您的结果不是 DataRows
的序列。 CopyToDataTable()
仅在 IEnumerable<T>
上可用,其中 T
是或源自 DataRow
(更多信息在此 link 中)。
正如您在引用的 link 中看到的那样,有一个解决方案。为此,您将需要实施基于反射的解决方法:
How to: Implement CopyToDataTable Where the Generic Type T Is Not a DataRow
您可以通过使用 where 语句分配数据行对象的 ItemArray 并检查它是否为空来在 from
语句中创建数据行集合。像这样:
static void Main(string[] args)
{
DataTable dt = new DataTable();
string path = "test_data.csv";
bool isFirstRowHeader = true;
dt = Utils.GetDataTableFromCsv(path, isFirstRowHeader);
var results = from row in dt.AsEnumerable()
group row by row.Field<string>("Group") into grp
let dr = dt.NewRow()
where (dr.ItemArray = new object[] { grp.Key, grp.Sum(r => r.Field<int>("Value")) }) != null
select dr ;
DataTable newDataTbl = results.CopyToDataTable();
}