none
Using the LINQ Dynamic Query Library with a DataTable RRS feed

  • Question

  • I've been starting to use the LINQ Dyanmic Query Library discussed here:

    http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

    This has been working pretty well but I was wondering if I could use it against the contents of a DataTable. I am wanting to select and order by a particular column that is not known until run time. 

    Since the extension methods that allow string use are for IQueryable, I am converting the DataTable to an IQueryable as so:

     

          EnumerableRowCollection lRows = sqTable.FinalDataTable.AsEnumerable();
          IQueryable IQueryableRows = lRows.AsQueryable();
    

     

    At this point I would like to use the Select and OrderBy in a query like so:

          IQueryable lQueryFinal = IQueryableRows.OrderBy("ComponentID")
                 .Select("ComponentID");
    

    But when I do, I get the following error: "No property or field 'ComponentID' exists in type 'DataRow'".

    Is there some way to use this method to select and order by the string in question or should I be using a different method altogether? Thanks you very much for any help on this issue.

     

    Friday, November 12, 2010 5:25 PM

Answers

  • Hello,

    Please try add reference to System.Data.Entity.dll.

    What's more, you could try to explicitly specify the field of your datatable as following:

    var result =  from row in FinalDataTable.AsEnumerable()
                       where row.Field<Int32>("ComponentID") >= 10
                       orderby row.Field<String>("ComponentName")
                       select row;

    Does this work for you?


    Best Regards,
    Roahn Luo
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com
    • Marked as answer by liurong luo Monday, November 22, 2010 12:14 PM
    Saturday, November 20, 2010 7:56 AM

All replies

  • Hello,

    Please try with the following snippet:

    IQueryable lQueryFinal = IQueryableRows.OrderBy(r=>r["ComponentID"]).Select(r=>r["ComponentID"]);


    Best Regards, Roahn Luo MSDN Subscriber Support in Forum If you have any feedback on our support, please contact msdnmg@microsoft.com
    Wednesday, November 17, 2010 9:54 AM
  • Thanks for the suggestion but it didn't seem to work. Using the line you suggested resulted in the following error:

    "Error    38    Cannot convert lambda expression to type 'string' because it is not a delegate type"

    By the way, I do have "Using System.Linq" in the code I am using.

     

    Thursday, November 18, 2010 9:26 PM
  • Hello,

    Please try add reference to System.Data.Entity.dll.

    What's more, you could try to explicitly specify the field of your datatable as following:

    var result =  from row in FinalDataTable.AsEnumerable()
                       where row.Field<Int32>("ComponentID") >= 10
                       orderby row.Field<String>("ComponentName")
                       select row;

    Does this work for you?


    Best Regards,
    Roahn Luo
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com
    • Marked as answer by liurong luo Monday, November 22, 2010 12:14 PM
    Saturday, November 20, 2010 7:56 AM