Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Simon Dingley 1474 posts 3431 karma points c-trib
    Jul 14, 2012 @ 08:57
    Simon Dingley
    0

    Where clause exceptions when comparing numerics

    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 185 posts 425 karma points
    Jul 15, 2012 @ 15:40
    gilad
    1

    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

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Jul 15, 2012 @ 15:55
    Simon Dingley
    0

    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 :(

     

  • Douglas Ludlow 210 posts 366 karma points
    Jul 16, 2012 @ 18:31
    Douglas Ludlow
    1

    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"])));
  • Simon Dingley 1474 posts 3431 karma points c-trib
    Jul 17, 2012 @ 09:12
    Simon Dingley
    0

    Good to know, thanks Douglas.

Please Sign in or register to post replies

Write your reply to:

Draft