C# Linq可枚举,可分组并聚合到datatable或list中

C# Linq可枚举,可分组并聚合到datatable或list中,c#,linq,C#,Linq,尝试使用LINQ根据组聚合检索记录,并将结果放入datatable foreach (var t in typeinfo) dttypes.Rows.Add(t.rsno, t.type, t.cnt); 表格(名称:rstable) Sql命令和输出: select rsno,type,count(type) as cnt from rstable group by rsno,type rsno type cnt ----------------- Rs01 1

尝试使用LINQ根据组聚合检索记录,并将结果放入datatable

foreach (var t in typeinfo)
    dttypes.Rows.Add(t.rsno, t.type, t.cnt);
表格(名称:rstable)

Sql命令和输出:

select rsno,type,count(type) as cnt from rstable group by rsno,type

rsno   type   cnt
-----------------
Rs01     1     2
Rs01     2     2
Rs02     5     4
-----------------  
Have created a datatable :

DataTable dttypes = new DataTable();
dttypes.Columns.Add("rsno", typeof(String));
dttypes.Columns.Add("type", typeof(int));
dttypes.Columns.Add("cnt", typeof(int));
尝试使用LINQ:

select rsno,type,count(type) as cnt from rstable group by rsno,type

rsno   type   cnt
-----------------
Rs01     1     2
Rs01     2     2
Rs02     5     4
-----------------  
Have created a datatable :

DataTable dttypes = new DataTable();
dttypes.Columns.Add("rsno", typeof(String));
dttypes.Columns.Add("type", typeof(int));
dttypes.Columns.Add("cnt", typeof(int));
这里dtresrep是一个数据表,它保存来自sql表的条目

var typeinfo = from typerow in dtresrep.AsEnumerable()
               group 1 by 
                     new { 
                             rsno = typerow.Field<String>("rsno"), 
                             type  = typerow.Field<int>("type") 
                         } into typegrp
               select new { 
                            typegrp.Key.rsno, 
                            typegrp.Key.type, 
                            cnt = typegrp.Count() 
                          };

这会引发强制转换异常。“指定的强制转换无效。”请指导。

它应该是这样的,因为它的
[System.InvalidCastException]={“指定的强制转换无效。”}
它与数据库和.net之间的类型不匹配有关。在这种情况下,它与
integer
类型有关

var typeinfo = (from typerow in dtresrep.AsEnumerable()
                 group typerow by 
                 new { 
                      resno = typerow["resno"] == DBNull.Value ? '' : typerow["resno"].toString() , 
                      type  = Convert.ToInt32(typerow["type"] == DBNull.Value ? 0 : typerow["type"]) 
                     } 
                    into typegrp
                    select new { 
                            typegrp.Key.resno, 
                            typegrp.Key.type, 
                            cnt = typegrp.Count() 
                          }).ToList();
为了在数据表中添加新行,代码应该如下

DataTable dttypes = new DataTable();
dttypes.Columns.Add("rsno", typeof(String));
dttypes.Columns.Add("type", typeof(int));
dttypes.Columns.Add("cnt", typeof(int));

foreach (var t in typeinfo)
{
  //you need to add row like this i.e. by calling NewRow() method
  //this can be issue in you code 
    row = dttypes.NewRow();
    row["rsno"] = t.rsno;
    row["type"] = t.type;
    row["cnt"] = t.cnt;
    dttypes.Rows.Add(row);
}

包括异常消息。[System.InvalidCastException]={“指定的强制转换无效。”}我已尝试使用“typerow”。根据某联机链接中的示例更改为1。这两种方法都会导致强制转换异常。指定的强制转换无效。是,我在LINQ查询中遇到错误itself@Priya-如果我不使用'ToList(),是否有可能从DataTable中获取null'最后,查询很好,但在foreach循环过程中出现错误。@Priya-我认为您必须检查空值…并相应地替换值。请检查我更新的linq。如果有问题,请告诉我