c# Datatable LINQ dynamic row.field编码的需要
本文关键字:编码 datatype LINQ Datatable dynamic row field | 更新日期: 2023-09-27 18:08:43
我有以下代码。数据来自mysql服务器。我使用dataadapter . fill(数据表)对象和方法来创建数据表CDMAData。列。每次数据范围不同时,数据类型就不同。我需要能够动态地指定数据类型。这可能吗?int>.
var CarrierCDMAPreResults = from row in CDMAData.AsEnumerable()
where Dates.Contains(row.Field<DateTime>("DateTime"))
group row by new { Cascade = row.Field<string>("Cascade"), Sector = row.Field<byte>("Sector"), Carrier = row.Field<byte>("Carrier") } into grp
select new
{
Cascade = grp.Key.Cascade,
Sector = grp.Key.Sector,
Carrier = grp.Key.Carrier,
Attempts = grp.Sum(r => r.Field <int> ("Attempts")),
Successes = grp.Sum(r => r.Field<int>("Successes")),
Blocks = grp.Sum(r => r.Field<int>("Blocks")),
Drops = grp.Sum(r => r.Field<int>("Drops")),
CDMAPE = grp.Sum(r => r.Field<decimal>("CDMA Primary Erlang"))
};
对于你的问题,一个非常简单的答案就是使用Int64。因为所有较低的值仍然有效。
然而,如果你的列开始变成小数和字符串,那么你就会遇到问题。
var CarrierCDMAPreResults = from row in CDMAData.AsEnumerable()
where Dates.Contains(row.Field<DateTime>("DateTime"))
group row by new { Cascade = row.Field<string>("Cascade"), Sector = row.Field<byte>("Sector"), Carrier = row.Field<byte>("Carrier") } into grp
select new
{
Cascade = grp.Key.Cascade,
Sector = grp.Key.Sector,
Carrier = grp.Key.Carrier,
Attempts = grp.Sum(r => r.Field<Int64> ("Attempts")),
Successes = grp.Sum(r => r.Field<Int64>("Successes")),
Blocks = grp.Sum(r => r.Field<Int64>("Blocks")),
Drops = grp.Sum(r => r.Field<Int64>("Drops")),
CDMAPE = grp.Sum(r => r.Field<decimal>("CDMA Primary Erlang"))
};