x First time here? Check out the FAQ
  • Avatar796posts1534karma

    Where clause exceptions when comparing numerics

    Simon Dingley started this topic July 14, 2012 @ 08:57, Go directly to the topic solution

    Can somebody please point out why the following is not a valid method of checking if a number falls within a particular range?

    products = products.Where(string.Format("price >= {0} and price is <= {1}", decimal.Parse(Request.Form["minPrice"]), decimal.Parse(Request.Form["maxPrice"])));

    I am getting a syntax error exception which is a System.Linq.Dynamic.ParseException type if that helps.

    Simon


  • gilad posted this reply July 15, 2012 @ 03:40

    What is the is in query? ( and price is <= {1} )

    Try this :

    products = products.Where(string.Format("price >= {0} && price <= {1}",decimal.Parse(Request.Form["minPrice"]),decimal.Parse(Request.Form["maxPrice"])));

    What is products? you can paste the code how do you get it and how you declare it?

    Hope that help.

    Cheers


  • Avatar796posts1534karma
    Comment with ID: 122048
    Simon Dingley posted this reply July 15, 2012 @ 03:55

    Thanks for pointing out my now very obvious school boy error, a case of not being able to see the wood for the trees! The issue was of course the "is" that had been added somehow in error :(

     


  • Avatar210posts355karma
    Comment with ID: 122138
    Douglas Ludlow posted this reply July 16, 2012 @ 06:31

    Just FYI, you can use the Where method without string.Format as it already parses the condition for parameters and replaces them, as in:

    products = products.Where("price >= @0 && price <= @1",
    decimal.Parse(Request.Form["minPrice"]), decimal.Parse(Request.Form["maxPrice"])));

  • Avatar796posts1534karma
    Comment with ID: 122185
    Simon Dingley posted this reply July 17, 2012 @ 09:12

    Good to know, thanks Douglas.


Please login or Sign up To post replies