none
String problem with dynamic Linq query RRS feed

  • Question

  • I have an ASP.NET 3.5 web page with various drop down lists that are used for assembling a string argument called QryString for a Linq to SQL query, using the Dynamic Linq Library. The line of code that generates the query is as follows (Recipients is an IQueryable):

    Recipients = myDBC.Entrants.Where(QryString)


    Unfortunately, this fails with the error message "Error: Sys.WebForms.PageRequestManagerServerErrorException: Expression of type 'Boolean' expected". The obvious assumption would be that I have constructed QryString incorrectly. However, if I disable the above line of code and simply display QryString in a label I get the result:

    "FirstName = @0 And Not (LastName = @1) Or Email1.Contains(@2)", "John", "Smith", "hotmail"


    That looks ok to me and if I simply copy that text into the following query it works as expected:

    Recipients = myDBC.Entrants.Where("FirstName = @0 And Not (LastName = @1) Or Email1.Contains(@2)", "John", "Smith", "hotmail")


    Why does this string argument work but a string variable containing the same data fail?

    David

    Wednesday, November 25, 2009 11:11 PM

Answers

  • Ooops! I've just realised my mistake. The Where function in my example was expecting to see four separate strings contained within the brackets. I was actually giving it a single string that merely looked like four separate strings.

    Problem now resolved.

    David
    • Marked as answer by haggis999 Thursday, November 26, 2009 12:03 PM
    Thursday, November 26, 2009 12:02 PM