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 1084 posts 2433 karma points
    Jul 14, 2012 @ 08:57
    Simon Dingley

    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.


  • gilad 185 posts 415 karma points
    Jul 15, 2012 @ 15: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.


  • Simon Dingley 1084 posts 2433 karma points
    Jul 15, 2012 @ 15:55
    Simon Dingley

    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 363 karma points
    Jul 16, 2012 @ 18:31
    Douglas Ludlow

    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 1084 posts 2433 karma points
    Jul 17, 2012 @ 09:12
    Simon Dingley

    Good to know, thanks Douglas.

Please Sign in or register to post replies

Write your reply to:

Draft is the community mothership for Umbraco, the open source cms. With a friendly forum for all your questions, a comprehensive documentation and a ton of packages from the community. This site is running Umbraco version 7.5.6