none
Executing query results from a typed dataset RRS feed

  • Question

  • Somewhat new to the LINQ to DataSet model.  I have the following query written:

     

    var query = from c in ds.COLLECTION

     

    join cu in ds.COLLECTION_UNIT

     

    on new { c.COLLECTION_ID, c.ACTIVE_YN }

     

    equals new { cu.COLLECTION_ID, cu.ACTIVE_YN }

     

    join cui in ds.COLLECTION_UNIT_ITEM

     

    on new { cu.UNIT_ID, cu.ACTIVE_YN }

     

    equals new { cui.UNIT_ID, cui.ACTIVE_YN }

     

    select new { c.COLLECTION_NAME, cu.UNIT_NAME, cui.ITEM_NAME };

    I would like to put this into a DataTable to bind to a ComboBox.  I have looked at some examples but I am not able to get the syntax correct.  I tried this:


    IEnumerable

     

    <DataRow> query2 = from c in ds.COLLECTION.AsEnumerable()

     

    join cu in ds.COLLECTION_UNIT.AsEnumerable()

     

    on new { X = c.Field<Decimal>("COLLECTION_ID"), Y = c.Field<String>("ACTIVE_YN") }

     

    equals new { X = cu.Field<Decimal>("COLLECTION_ID"), Y = cu.Field<String>("ACTIVE_YN") }

     

    join cui in ds.COLLECTION_UNIT_ITEM.AsEnumerable()

     

    on new { X = cu.Field<Decimal>("UNIT_ID"), Y = cu.Field<String>("ACTIVE_YN") }

     

    equals new { X = cui.Field<Decimal>("UNIT_ID"), Y = cui.Field<String>("ACTIVE_YN") }

     

    select new { X = c.Field<String>("COLLECTION_NAME"),

    Y = cu.Field<

    String>("UNIT_NAME"),

    Z = cui.Field<

    String>("ITEM_NAME") };


    DataTable

     

    dt = query2.CopyToDataTable<DataRow>();

    I get the following error on the 'join cui' line:

    Error 1 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?) 

    What am I doing wrong?  Also I don't like the syntax having to use  cui.Field<String>("ITEM_NAME" vs. cui.UNIT_ID.    
    Isn't that the point of using a strongly typed dataset?

    Thanks.
    Saturday, May 23, 2009 4:20 AM

All replies

  • You might get better responses in the DataSet forum: http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataset/threads
    Blog - http://blogs.rev-net.com/ddewinter/ Twitter - @ddewinter
    Tuesday, May 26, 2009 1:20 AM
    Answerer
  • The signature of CopyToDataTable is as follows:

    public static DataTable CopyToDataTable<T>(
        this IEnumerable<T> source
    )
    where T : DataRow

    In your case, your statement dt = query2.CopyToDataTable<DataRow> is effectively saying that the source type is a DataRow, which it isn't. It is an anonymous type. Thus the error message indicating that you can't implicitly convert from AnonymousType to DataRow. Consider leaving the generic <T> qualifier off and let the inferencing take care of the typing for you as follows:

    dt = query2.CopyToDataTable();

    As for the Field<T>("fieldname") syntax, you should be able to use the strongly typed names in your query. It might help to check out our free chapter 14 which deals with LINQ to Datasets at http://www.manning.com/marguerie/.

    Jim Wooley
    www.ThinqLinq.com
    http://www.LinqInAction.net - "LINQ In Action", The book is now available. Don't wait for the movie
    Thursday, May 28, 2009 11:18 PM
    Moderator