2017-06-02 299 views
1

不能将类型'System.Collections.Generic.List<AnonymousType#1>'隐式转换为'System.Collections.Generic.IEnumerable<System.Data.DataRow>'。一个显式转换存在(是否缺少强制转换?)C#不能隐式转换类型'System.Collections.Generic.List <AnonymousType#1>'

我有两个数据表称为数据2,数据3, 我想将数据输入到数据3 group by后。 如何修改我的代码?

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

回答

4

投影:

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")) 
    } 

意味着你每行创建,一个匿名类型(由编译器生成的类)的实例; 不是 a DataRow。所以当你.ToList()那,你会得到一个List<T>其中T是匿名类型。你可以这样做var temp = ...分配给某个东西,但它仍然不会和DataRow有什么关系。你需要(之一):

  • 没有使用DataTable/DataRow(严重的是,他们是可怕的;其它任何东西最好)
  • 手动填充DataTable通过遍历你的对象,创建行和分配值
  • 使用的工具,它可以自动上述观点
+0

谢谢你的帮助 –

1

试试这个:

 var 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 =ConvertToDataTable(temp); 


public DataTable ConvertToDataTable<T>(IList<T> data) 
    { 
     PropertyDescriptorCollection properties = 
      TypeDescriptor.GetProperties(typeof(T)); 

     DataTable table = new DataTable(); 

     foreach (PropertyDescriptor prop in properties) 
      table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType); 

     foreach (T item in data) 
     { 
      DataRow row = table.NewRow(); 
      foreach (PropertyDescriptor prop in properties) 
       row[prop.Name] = prop.GetValue(item) ?? DBNull.Value; 
      table.Rows.Add(row); 
     } 
     return table; 
    } 
+0

谢谢你的帮助 –

相关问题