Question : How do I combine 2 List<KeyValuePair<,>>'s into a DataTable using LINQ?

Hi:

I'm trying to combine 2 data sources into a DataTable using LINQ and CopyToDataTable<DataRow>.
The data sources are of type List<KeyValuePair<x,y>>.



// Define the table & columns
DataTable Travelers;

Travelers = new DataTable("Travelers");
DataColumn column;
column = new DataColumn("FullName", Type.GetType("System.String"));
Travelers.Columns.Add(column);
column = new DataColumn("IsFreePlace", Type.GetType("System.Boolean"));
Travelers.Columns.Add(column);
column = new DataColumn("IndividualPaymentAmount", Type.GetType("System.Decimal"));
Travelers.Columns.Add(column);


////////////////////////////////////////////////////////////////////////////////////////////////////
// Query to consolidate 2 data sources into a LINQ query
//
// Data sources have the following form:
//
//  ListOfItems -->  List<KeyValuePair<InfoTypeA, int>>
//
//  Information -->  List<KeyValuePair<InfoTypeB, decimal>>
//
//
////////////////////////////////////////////////////////////////////////////////////////////////////
var query = (from pax in ListOfItems
                         from pInfo in Information
                          where pax.Value == pInfo.Key.TravelerID
                          select new { pax.Key.FirstName,
                                        pax.Key.LastName,
                                        pax.Key.IsFreePlace,
                                        pInfo.Value });


// Store this consolidated info into a table
Travelers = query.CopyToDataTable<DataRow>();


////////////////////////////////////////////////////////////////////////////////////////////////////
// When I try the above, I get the following compiler error:
////////////////////////////////////////////////////////////////////////////////////////////////////
Error      31      Instance argument: cannot convert   from 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable<System.Data.DataRow>'

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

////////////////////////////////////////////////////////////////////////////////////////////////////
Next, I tried the following:
////////////////////////////////////////////////////////////////////////////////////////////////////
 IEnumerable<DataRow> query = (from pax in ListOfItems
                         from pInfo in Information
                          where pax.Value == pInfo.Key.TravelerID
                          select new { pax.Key.FirstName,
                                        pax.Key.LastName,
                                        pax.Key.IsFreePlace,
                                        pInfo.Value });

////////////////////////////////////////////////////////////////////////////////////////////////////
// When I try the above, I get the following compiler error:
////////////////////////////////////////////////////////////////////////////////////////////////////
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable<System.Data.DataRow>'. An explicit conversion exists (are you missing a cast?)



I can easily just use the 1st method, and then iterate with a foreach loop, but I'd
like to try to utilize the CopyToDatTable() method of the System.Data.DataTableExtensions.


Thanks in advance,
JohnB

Answer : How do I combine 2 List<KeyValuePair<,>>'s into a DataTable using LINQ?

Hi jxbma;

The query you are trying to execute and fill its results into a DataTable with will not work. The syntax for the CopyToDataTable is as follows :

Public Shared Function CopyToDataTable(Of T As DataRow) ( _
      source As IEnumerable(Of T) _
) As DataTable

But what you have is the following :

var query = (from pax in ListOfItems
                   from pInfo in Information
                   where pax.Value == pInfo.Key.TravelerID
                   select new { pax.Key.FirstName,
                                        pax.Key.LastName,
                                        pax.Key.IsFreePlace,
                                        pInfo.Value });

Which the type of the query is of Anonymous type as can be seen from the select clause where you use the keyword new without a type. When you query two List the results is of some List or IEnumerable. There is no function that will convert a List to a DataRow of a DataTable.

The solution will be to loop through the results and create a DataRow from the DataTable for each element of the result set.

Fernando

Random Solutions  
 
programming4us programming4us