[英]GroupBy column using LINQ
我想使用followng方法按數據表分組。 但是我不能選擇包含字符串的列
var dtTAConvertList =
(from dr1 in dtTAConvert.AsEnumerable()
group dr1 by dr1.Field<DateTime>("SAMPLE_TIME") into g
select new
{
SAMPLE_TIME = g.Key,
WAIT_CLASS = g.Field<string>("WAIT_CLASS"), // ==> I get error in this line
COUNT = Math.Round(g.Sum(h => h.Field<double>("COUNT")) / 15.0, 2),
});
引發異常
“ IGrouping”不包含“字段”的定義,最佳擴展方法重載“ DataRowExtensions.Field(DataRow,string)”需要類型為“ DataRow”的接收器
如何選擇該數據表中已存在的字符串列?
由於每個組中有很多出現的Field<string>("WAIT_CLASS")
,因此您必須與計數過程大致相同,只是可能要檢索第一個出現的位置:
var dtTAConvertList =
(from dr1 in dtTAConvert.AsEnumerable()
group dr1 by dr1.Field<DateTime>("SAMPLE_TIME") into g
select new {
SAMPLE_TIME = g.Key,
WAIT_CLASS = g.First().Field<string>("WAIT_CLASS"),
COUNT = Math.Round(g.Sum(h => h.Field<double>("COUNT")) / 15.0, 2),
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.