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

本文关键字:linq 结果 合并 方法 | 更新日期: 2023-09-27 18:08:22

我得到了这个代码

但是我得到了以下错误:

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

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

这些不是datarow,所以你不能将它们复制到DataTable中。

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

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