Copied to clipboard

Flag this post as spam?

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


  • Robin Nicholl 137 posts 277 karma points
    Nov 06, 2017 @ 14:02
    Robin Nicholl
    0

    Convert string to float number

    I need to build a selection containing all child nodes of a particular type where the 'price' property (which is a string) is <= a variable (also a string).

    This will obviously do a string match...

    var selection = myNode.GiftTypes.Where("price <= \""+costlimit+"\"")
    

    ... but I need to convert 'price' and 'costlimit' to float numbers so that I can do...

    price <= costlimit // e.g. 8.95 <= 20
    

    I've tried all sorts of things, like price.AsInt(), Int32.Parse(price), but everything throws an error.

    Thanks

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Nov 06, 2017 @ 14:05
    Alex Skrypnyk
    0

    Hi Robin

    Try this code:

    var selection = myNode.GiftTypes..Where(x => x.GetPropertyValue<float>("price") <= costlimit));
    

    Thanks,

    Alex

  • Robin Nicholl 137 posts 277 karma points
    Nov 06, 2017 @ 14:12
    Robin Nicholl
    0

    Thanks, Alex, for such a rapid reply, but this gives an error:

    Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
    
  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Nov 06, 2017 @ 14:21
    Alex Skrypnyk
    0

    Show, please, all code

  • Robin Nicholl 137 posts 277 karma points
    Nov 06, 2017 @ 14:38
    Robin Nicholl
    0
    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    var gender = Request["gender"];         @* eg "male" *@
    var relation = Request["relation"];     @* eg "family,friend" *@
    var age = Request["age"];               @* eg "child,teen" *@
    var interests = Request["interests"];   @* eg "food" *@
    var costlimit = Request["costlimit"];   @* eg "100" *@
    
    var mainSelection = CurrentPage.Descendants().Where("Level == "+sublevel+"").Where("Visible");
    
    @foreach( var subNode in mainSelection ) {
    
        var giftSelection = subNode.GiftTypes
                .Where("gender.Contains(\""+gender+"\")")
                .Where("relation.Contains(\""+relation+"\")")
                .Where("age.Contains(\""+age+"\")")
                .Where("interests.Contains(\""+interests+"\")")
                .Where(x=>x.GetPropertyValue<float>("price") <= 100);<<<costlimit hardcoded for testing
    
        foreach( var gift in giftSelection ) {
    
            <div>
                ... do stuff
            </div>
    
        }
    
    }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies