0

Hello I'm trying to copy the following linq results to a datatable. The only examples I see of copytodatatable is using the query format, and not the extension methods. Wondering if anyone knows how to use it with the extension methods (I've tried casting the results to IEnumerable datarow but it didn't work).

DataTable dtItemPricingBreakDown = 
    SiteHelper.getItemPricingBreakDown(CustomerId, 
                                        PriceBookID, 
                                        deliveryZip, 
                                        dtItems, 
                                        db, 
                                        departmentId);

dtItemPricingBreakDown.AsEnumerable()
                      .GroupBy(i => new { 
                                            sku = i.Field<string>("sku"), 
                                            deptid = i.Field<int>("department_id") 
                                         })
                      .Select(group => new 
                                        { 
                                            sku = group.Key.sku, 
                                            deptid = group.Key.deptid, 
                                            cnt = group.Count() 
                                        });

Update

Better late than never, sorry for the delay in response. My actual issue appears to be for both the query syntax and the extension methods.

Something like this works according to msdn
(http://msdn.microsoft.com/en-us/library/bb386921(v=vs.110).aspx):

// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);

DataTable orders = ds.Tables["SalesOrderHeader"];
DataTable details = ds.Tables["SalesOrderDetail"];

var query =
    from order in orders.AsEnumerable()
    join detail in details.AsEnumerable() on order.Field<int>("SalesOrderID") equals detail.Field<int>("SalesOrderID")
    where order.Field<bool>("OnlineOrderFlag") == true
    && order.Field<DateTime>("OrderDate").Month == 8
    select new
    {
        SalesOrderID = order.Field<int>("SalesOrderID"),
        SalesOrderDetailID = detail.Field<int>("SalesOrderDetailID"),
        OrderDate = order.Field<DateTime>("OrderDate"),
        ProductID = detail.Field<int>("ProductID")
    };

DataTable orderTable = query.CopyToDataTable(); 

But when I try this...

var query = from exx in dtItemPricingBreakDown.AsEnumerable()
            group exx by new { sku = exx.Field<string>("sku"), companyId = exx.Field<int>("department_id") } into grp
            select new { sku = grp.Key.sku, DepartmentID = grp.Key.companyId, Cnt = grp.Count() };

DataTable dt3 = query.CopyToDataTable();

I get this exception: "No implicit reference conversion from anonymoustype1 to system.data.datarow". I've tried doing what they did with the dataset in the msdn example as well and I still get the error. Extension method wise I was trying something like this...and still got the same exception.

DataTable dt3 = dtItemPricingBreakDown.AsEnumerable().GroupBy(i => new
                {
                    sku = i.Field<string>("sku"),
                    deptid = i.Field<int>("department_id")
                }).Select(group => new
                {
                    sku = group.Key.sku,
                    DepartmentID = group.Key.deptid,
                    cnt = group.Count()
                }).Cast<DataRow>().CopyToDataTable();
mgmedick
  • 686
  • 7
  • 23
  • Are you using C#? You are tring to insert the data to a sql table? – Ezi May 13 '14 at 01:03
  • Take a look at [this](http://stackoverflow.com/a/7094652) and [this](http://msdn.microsoft.com/en-us/library/bb669096(v=vs.110).aspx) – thepirat000 May 13 '14 at 01:26
  • There is no difference between query syntax and extension methods. Which examples did you see? Also, *how* did it not work? – Gert Arnold May 13 '14 at 06:50

0 Answers0