2017-06-02 136 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

謝謝你的幫助 –

相關問題