如何在合并方法中使用linq的结果

时间:2011-09-13 14:51:39

标签: c# .net ado.net

我收到了这段代码

但是我收到了以下错误:

Error   1   Instance argument: cannot convert from 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable<System.Data.DataRow>'   

Error   2   'System.Collections.Generic.IEnumerable<AnonymousType#1>' does not contain a definition for 'CopyToDataTable' and the best extension method overload 'System.Data.DataTableExtensions.CopyToDataTable<T>(System.Collections.Generic.IEnumerable<T>)' has some invalid arguments 



 var distinctRows = (from DataRow dRow in vwBusPark.toDataTable().Rows
                                    select new { col1 = dRow["BusinessParkID"], col2 = dRow["BusinessParkID"] }).Distinct();

                dataSet.BusinessPark.Merge(distinctRows.CopyToDataTable<DataRow>(), true, System.Data.MissingSchemaAction.Ignore);

我试过了:

dataSet.BusinessPark.Merge(vwBusPark.toDataTable().Rows.Except(DataRowComparer<UC010_WizardStepBusinessParkDataSet.BusinessParkRow>.Default).CopyToDataTable(), true, System.Data.MissingSchemaAction.Ignore);

但我得到了这两个错误:

Error   2   'System.Collections.Generic.IEnumerable<AnonymousType#1>' does not contain a definition for 'CopyToDataTable' and the best extension method overload 'System.Data.DataTableExtensions.CopyToDataTable<T>(System.Collections.Generic.IEnumerable<T>)' has some invalid arguments 

Error   1   Instance argument: cannot convert from 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable<System.Data.DataRow>'

1 个答案:

答案 0 :(得分:1)

那些不是DataRows,因此您无法将它们复制到DataTable。

假设BusinessParkvwBusPark具有相同的列名,则根本不需要LINQ查询;相反,你可以写

vwBusPark.toDataTable().Rows.Except(...).CopyToDataTable()