0
votes

Trying to return a Linq group by query into a DataTable. Getting the 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?)

The I am querying a DataTable named Vendors where the data would be as follows:

Vendor         Name 
654797         Lowes
897913         Home Depot
800654         Waffle House

The Vendor is stored as char(6) in the DB and name as char as well... don't ask me why, I just work here :)

    DataTable VendorsDT = New DataTable();
    DataColumn VenName = VendorsDT.Columns.Add("Name", typeof (string));
    DataColumn VenCode = VendorsDT.Columns.Add("Vendor", typeof(string));


    IEnumerable<DataRow> Vendors = from row in alertsDT.AsEnumerable()
                                   group row by new { Name = row.Field<string>("Name"), Vendor = row.Field<string>("Vendor") } into z
                                   select new
                                   {
                                        Name = z.Key.Name,
                                        Vendor = z.Key.Vendor,                        

                                   };



    VendorsDT = Vendors.CopyToDataTable();
2

2 Answers

0
votes

This is not going to work, as you are projecting your original query of alertsDT into an anonymous type, which would not be able to be referenced by your IEnumerable<DataRow> Vendor variable, because your query is not a sequence of DataRows.

Given that you are performing a grouping, and that you have also already set up your VendorsDT table with the desired columns, the path of least resistance is to fix your Vendor variable type (use var for type inference) and then loop over the result to populate your second table.

var Vendors = /* your unchanged query omitted */ 

foreach (var item in Vendors) 
{
    VendorsDT.Rows.Add(item.Name, item.Vendor);
}

As a note, I've used your variable names, although it is convention in C# to use lower case letters to start local variable names, with upper case typically left for method names, properties, etc. So you would favor vendors over Vendors and vendorsDT (or vendorsTable) over VendorsDT, for example.

-1
votes

This is a more linq way of doing

  var query = from row in alertsDT.AsEnumerable()
                      group row by new { Name = row.Field<string>("Name"), Vendor = row.Field<string>("Vendor") } into z
                      select new 
                      {
                        Name=  z.Key.Name,
                        Vendor=  z.Key.Vendor,

                      };
        VendorsDT = query.CopyToDataTable();

Here define the extension method 'CopyToDataTable()' as specified in the following MSDN article.

The CopyToDataTable method takes the results of a query and copies the data into a DataTable, which can then be used for data binding. The CopyToDataTable methods, however, only operate on an IEnumerable source where the generic parameter T is of type DataRow. Although this is useful, it does not allow tables to be created from a sequence of scalar types, from queries that project anonymous types, or from queries that perform table joins.