【问题标题】:Linq Enumerable with Grouping and Aggregate into datatable or listLinq Enumerable with Grouping and Aggregate into datatable or list
【发布时间】:2018-03-19 14:08:06
【问题描述】:

尝试使用 LINQ 检索基于组聚合的记录并将结果放入数据表中。

表(名称:rstable)

RSNo        Type
------------------
Rs01      |  1  |     
Rs02      |  5  |   
Rs01      |  2  |  
Rs01      |  1  |  
Rs02      |  5  |  
Rs02      |  5  |  
Rs01      |  2  |  
Rs02      |  5  |      
------------------ 

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
-----------------  

尝试使用 LINQ:

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() 
                          };

然后尝试放入数据表中。

foreach (var t in typeinfo)
    dttypes.Rows.Add(t.rsno, t.type, t.cnt);

这会引发 Cast 异常。 “指定的演员表无效。”请指导。

【问题讨论】:

  • 包含异常信息。
  • [System.InvalidCastException] = {"指定的强制转换无效。"}

标签: c# linq


【解决方案1】:

应该是这样的,因为它的[System.InvalidCastException] = {"Specified cast is not valid."}它的错误与DataBase和.net moslty之间的类型不匹配有关,在这种情况下它与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);
}

【讨论】:

  • 我已经尝试过使用“typerow”。根据在线某些链接中的示例更改为 1。这两种方式都会导致 Cast Exception。 "指定的演员表无效。
  • 是的,我收到 LINQ 查询本身的错误
  • @Priya - 你有没有可能从数据表中得到 null
  • 如果我最后不使用 'ToList()' 则查询没问题,但在 foreach 循环期间发生错误。
  • @Priya - 我认为你必须检查空值......并相应地替换值请检查我更新的 linq..如果有问题请告诉我
猜你喜欢
  • 2022-12-01
  • 1970-01-01
  • 2017-11-01
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
  • 2012-11-06
  • 2017-10-19
  • 2021-02-02
相关资源
最近更新 更多