Microsoft Developer Network > Forums Home > Archived Forums Forums > LINQ Project General > Question about Dynamic Expression API (System.Linq.Dynamic)
Ask a questionAsk a question
 

AnswerQuestion about Dynamic Expression API (System.Linq.Dynamic)

  • Monday, August 04, 2008 6:09 PMs441 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi

     

    I am trying to understand and utilize the Dynamic Expression API  available as System.Linq.Dynamic.

     

    If I have a query like below:

    var query =
        db.Customers.
        Where("City = @0", "London").
        OrderBy("CompanyName").
        Select("new(CompanyName as Name, Phone)");

     

    with the Dynamic Expression API, would I be able to specify LIKE operator on the City field?

     

    I want to do something like this:

     

    var query =
        db.Customers.
        Where("City LIKE @0", "L").
        OrderBy("CompanyName").
        Select("new(CompanyName as Name, Phone)");

     

    How can I do this?

     

    Thanks much in advance!

     

Answers

  • Monday, August 04, 2008 6:33 PMs441 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Got it.

     

     

    var query =
        db.Customers.
        Where("City.StartsWith(\"L\")").
        OrderBy("CompanyName").
        Select("new(CompanyName as Name, Phone)");

  • Monday, August 04, 2008 7:12 PMs441 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Guess what? That's possible too.

     

     

       string condition1 = "FirstName.StartsWith(\"" + textBox1.Text + "\")";
       string condition2 = "FirstName.StartsWith(\"" + textBox2.Text + "\")";
        var q = db.Employees.Where(condition1 + " or " + condition2).Select("new(FirstName,LastName)");

All Replies

  • Monday, August 04, 2008 6:33 PMs441 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Got it.

     

     

    var query =
        db.Customers.
        Where("City.StartsWith(\"L\")").
        OrderBy("CompanyName").
        Select("new(CompanyName as Name, Phone)");

  • Monday, August 04, 2008 6:45 PMs441 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Another roadblock.

     

    Is it possible to have the OR operator between filter criteria?

     

    I am trying to do something like this:

     

    var query =
        db.Customers.
        Where("City == @0 OR City == @1","London","Paris").
        OrderBy("CompanyName").
        Select("new(CompanyName as Name, Phone)");

     

     

  • Monday, August 04, 2008 7:12 PMs441 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Guess what? That's possible too.

     

     

       string condition1 = "FirstName.StartsWith(\"" + textBox1.Text + "\")";
       string condition2 = "FirstName.StartsWith(\"" + textBox2.Text + "\")";
        var q = db.Employees.Where(condition1 + " or " + condition2).Select("new(FirstName,LastName)");

  • Wednesday, January 28, 2009 7:13 AMjeyaseelan2 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    S441,

    I went through your code, its good. I have the similar situation but i expect the dynamic query to xml file, could you please tell me how to write the dynamic query to an xml file.