none
Dynamic Linq to DataSet RRS feed

  • Question

  • Hi everyone,

    Do someone know if I can make a dynamic linq to dataset query in a form like this?

    var query =
        db.Customers.
        Where("City = @0 and Orders.Count >= @1", "London", 10).
        OrderBy("CompanyName").
        Select("new(CompanyName as Name, Phone)");

    I ask it because it returns this error on where clause:

    'Beauty.beautyDbDataSet.anagraficaDataTable' does not contain a definition for 'Where' and the best extension method overload 'System.Linq.Dynamic.DynamicQueryable.Where(System.Linq.IQueryable, string, params object[])' has some invalid arguments

    Can someone give me some suggestions? thank you

    Tuesday, July 31, 2012 4:35 PM

Answers

  • I may not understood your question correctly. But you may try like this.

    var query=db.Customers.Where(x=>x.City="NY" && x.Orders.Count()>1).OrderBy(x=>x.CompanyName).Select(x=>new{x.CompanyName,x.Phone});

    Though it is in lamda style linq has specific syntax rule. Your query has to be well formatted to linq syntax.

    Note the above query is an expensive one. You may try to make it simpler if possible.

    However LinQ does not  support dynamic queries in the sense it accepts parameters at runtime, but the query string can't be build at  runtime. LinQ objects and relationships are defined at design time itself.

    Good Luck!


    Tuesday, July 31, 2012 7:15 PM
  • Hi,

    I guess you could write a good stored procedure to handle this requirement on Sql Server side and call it by linq. For more information please refer to:

    l  http://weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx

    Hope this could help you.

    Best Regards,


    Please mark this as answer if it helps with this issue!

    Tuesday, August 7, 2012 7:24 AM

All replies

  • I may not understood your question correctly. But you may try like this.

    var query=db.Customers.Where(x=>x.City="NY" && x.Orders.Count()>1).OrderBy(x=>x.CompanyName).Select(x=>new{x.CompanyName,x.Phone});

    Though it is in lamda style linq has specific syntax rule. Your query has to be well formatted to linq syntax.

    Note the above query is an expensive one. You may try to make it simpler if possible.

    However LinQ does not  support dynamic queries in the sense it accepts parameters at runtime, but the query string can't be build at  runtime. LinQ objects and relationships are defined at design time itself.

    Good Luck!


    Tuesday, July 31, 2012 7:15 PM
  • Hi,

    I guess you could write a good stored procedure to handle this requirement on Sql Server side and call it by linq. For more information please refer to:

    l  http://weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx

    Hope this could help you.

    Best Regards,


    Please mark this as answer if it helps with this issue!

    Tuesday, August 7, 2012 7:24 AM