C# 不能隐式转换类型“System.Collections.Generic.List<AnonymousType#1>”

维克多·林

无法将类型隐式转换'System.Collections.Generic.List<AnonymousType#1>''System.Collections.Generic.IEnumerable<System.Data.DataRow>'. 存在显式转换(您是否缺少演员表?)

我有两个数据表叫做Data2,Data3,我想在分组后将数据输入到Data3中。如何修改我的代码?

IEnumerable<DataRow> temp = (from p in Data2.AsEnumerable()
                              group p by new
                              {
                                  ID = p.Field<string>("ID"),
                                  StyleID = p.Field<string>("StyleID"),
                                  BrandID = p.Field<string>("BrandID"),
                                  SeasonID = p.Field<string>("SeasonID"),
                                  Article = p.Field<string>("Article"),
                                  PatternCode = p.Field<string>("PatternCode")
                              } into g
                              select new
                              {
                                  ID = g.Key.ID,
                                  StyleID = g.Key.StyleID,
                                  BrandID = g.Key.BrandID,
                                  SeasonID = g.Key.SeasonID,
                                  Article = g.Key.Article,
                                  PatternCode = g.Key.PatternCode,
                                  QTY = g.Sum(p => p.Field<int>("QTY"))
                              }).ToList();

                 Data3 = temp.CopyToDataTable();
马克·格拉维尔

投影:

  select new
  {
      ID = g.Key.ID,
      StyleID = g.Key.StyleID,
      BrandID = g.Key.BrandID,
      SeasonID = g.Key.SeasonID,
      Article = g.Key.Article,
      PatternCode = g.Key.PatternCode,
      QTY = g.Sum(p => p.Field<int>("QTY"))
  }

意味着您正在每行创建一个匿名类型的实例(由编译器生成的类);没有一个DataRow所以当你这样做时.ToList(),你会得到一个List<T>whereT是匿名类型。你可以var temp = ...指定列表中的东西,但它仍然不会是什么关系DataRow您需要(其中之一):

  • 不使用DataTable/ DataRow(说真的,它们很糟糕;几乎其他任何东西都更可取)
  • DataTable通过循环对象、创建行和分配值来手动填充
  • 使用自动化上述点的工具

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法将类型'System.Collections.Generic.List <AnonymousType#1>'隐式转换为'System.Collections.Generic.List

无法将类型'System.Collections.Generic.List <AnonymousType#1>'隐式转换为'System.Collections.Generic.List <Model.tblLaborBankAccount>'

无法将类型'System.Collections.Generic.List <AnonymousType#1>'隐式转换为'System.Collections.Generic.List <FirstApp.Model.TeamDetails>

无法将类型'System.Collections.Generic.List <AnonymousType#1>'隐式转换为'System.Collections.Generic.List <string>'

无法将类型'System.Collections.Generic.Lis <AnonymousType#1>'隐式转换为'System.Collections.Generic.List <AdventureCycle.Models.Product>'

不能将类型 system.collections.generic.list 隐式转换为 system.collections.generic.IEnumerable

无法将类型'System.Collections.IList'隐式转换为'System.Collections.Generic.List

无法将类型System.Collections.Generic.Ienumerable <string>隐式转换为system.Linq.IOrderedEnumerable <AnonymousType#1>

无法将类型“System.Collections.Generic.List<<匿名类型:>>”隐式转换为“System.Collections.Generic.List”

无法将类型'System.Collections.Generic.IEnumerable <AnonymousType#1>'隐式转换为'System.Collections.Generic.IEnumerable <T>

无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List<Model.Room”

无法将类型System.Collections.Generic.List <DerivedDataClass>隐式转换为System.Collections.Generic.List <BaseDataClass>

无法将类型System.Collections.Generic.List <>隐式转换为System.Collections.Generic.List <>

Web API 无法隐式转换类型“System.Collections.Generic.List<<匿名类型:

无法隐式转换类型“System.Collections.Generic.List<<匿名类型

无法将类型'System.Linq.IQueryable'隐式转换为'System.Collections.Generic.List

错误“无法将类型“System.Collections.Generic.IEnumerable<int>”隐式转换为“System.Collections.Generic.List<int>”

无法将类型'System.Collections.Generic.List <>'隐式转换为'System.Collections.Generic.IList <>'

无法将类型'System.Collections.Generic.List <Apps.RootObject>'隐式转换为'Apps'

无法将类型隐式转换为'System.Collections.Generic.List

无法将类型“ void”隐式转换为“ System.Collections.Generic.List”以用于返回语句

无法在 asp.net MVC 中隐式转换类型“System.Collections.Generic.List”

无法将匿名类型隐式转换为 System.Collections.Generic.List

无法将类型'int'隐式转换为'System.Collections.Generic.List <QuickTest.Stock>'

无法将类型“System.Collections.Generic.List<Object>”隐式转换为“string”

无法将类型'System.Collections.Generic.List'隐式转换为'string'

无法隐式转换类型'int?' 到'System.Collections.Generic.List <Model>

无法将类型“字符串”隐式转换为“System.Collections.Generic.List<int>”

无法将类型“System.Collections.Generic.List<string>”隐式转换为“string”