Copied to clipboard

Flag this post as spam?

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


  • Paul 184 posts 646 karma points
    Jul 10, 2018 @ 13:39
    Paul
    0

    Using LINQ Orderbys with string parameters

    Hi all,

    I've a query like so:

    var currFolder = Umbraco.TypedMedia(folders);  
    IEnumerable<IPublishedContent> fileList;  
    fileList = currFolder.Children.OrderBy(sortbyoption);
    

    This works, but the call in the OrderBy to a variable is dynamic; which I want to avoid so I was trying....

    var currFolder = Umbraco.TypedMedia(folders);
    IEnumerable<IPublishedContent> fileList;  
    fileList = currFolder.Children.OrderBy(x => x.GetPropertyValue(sortbyoption));
    

    The latter doesn't work. Is there some limitation on OrderBy that prevents me from using strongly typed syntax? I just want to pull in the variable :-S

    Any help appreciated as usual folks!

    EDIT: Umb 7.10.4

  • Paul 184 posts 646 karma points
    Jul 10, 2018 @ 13:54
    Paul
    0

    Just to add, when sticking break points I can see that .OrderBy(x => x.GetPropertyValue(sortbyoption) brings back the correct information, the Linq statement just seems to ignore it.

  • Paul 184 posts 646 karma points
    Jul 11, 2018 @ 13:23
    Paul
    0

    No one have any ideas on this one?

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Jul 18, 2018 @ 09:50
    Alex Skrypnyk
    100

    Hi Paul

    I think the issue is in a type of property value, please, provide a type of property value, "string" for example:

    fileList = currFolder.Children.OrderBy(x => x.GetPropertyValue<string>(sortbyoption));
    

    GetPropertyValue() - returns an object type, that is hard to compare and sort

    Thanks,

    Alex

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Jul 23, 2018 @ 23:19
    Alex Skrypnyk
    0

    Hi Paul

    Did you solve the issue?

    Alex

  • Paul 184 posts 646 karma points
    Aug 02, 2018 @ 07:35
    Paul
    0

    Hi Alex,

    Unfortunately not; It appears my issue is with the line:

    fileList = currFolder.Children.OrderBy(x => x.GetPropertyValue<string>(sortbyoption));
    

    When I use this I can see, using breakpoints in VS that it's pulling in the correct value (e.g. Name). But the query doesn't seem register this. When I hardcode it there is no issue and it works so...

    This works:

    fileList = currFolder.Children.OrderBy(x => x.Name);
    

    But this does not:

    fileList = currFolder.Children.OrderBy(x => x.GetPropertyValue<string>(sortbyoption));
    

    If I pull out the value for sortbyoption on the same page it returns "Name" which is correct. I'm very puzzled by why this isn't working.

  • Paul 184 posts 646 karma points
    Aug 02, 2018 @ 07:37
    Paul
    0

    As mentioned in post #1, if I use dynamics it works as well. So it appears for some reason LINQ doesn't like me dropping in a property value into the query, even though my syntax appears to be fine?

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Aug 02, 2018 @ 08:24
    Alex Skrypnyk
    0

    What is "sortbyoption" in this line?

    fileList = currFolder.Children.OrderBy(x => x.GetPropertyValue<string>(sortbyoption));
    
  • Paul 184 posts 646 karma points
    Aug 02, 2018 @ 08:36
    Paul
    0

    sortbyoption is derived from a switch, so contains a string of "Name".

    This, as I've just found appears to be the issue as LINQ’s OrderBy() method doesn’t accept string parameters, only lambda expressions apparently. That'll explain why everything I'm trying fails!

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Aug 02, 2018 @ 08:37
    Alex Skrypnyk
    0

    There is no "Name" property in Umbraco Nodes, it's reserved property that you can call directly like x.Name

  • Paul 184 posts 646 karma points
    Aug 02, 2018 @ 09:12
    Paul
    0

    Looks like I've not made myself very clear here !

    The code is a LeBlender grid editor that allows editors to pick a folder/s and then select ordering/sorting on them and drop them into a page.

    So "Name" starts life in the LeBlender editor as "File Name", it's then switched to be a string of "Name"; at this point it's still just a string variable.

    I then want to take that and integrate it into my LINQ query so it will sort by the property x.Name, so I'm trying to create the LINQ query based upon a few variables in the code block. At no point am I looking for an Umbraco Node Name; I'm just taking a string variable (with a value of "Name") and trying to drop it in mid-LINQ query to create x.Name.

    Hopefully that makes a little more sense now. Thanks for your continued help Alex.

  • Paul 184 posts 646 karma points
    Aug 07, 2018 @ 10:19
    Paul
    0

    Has anyone successfully used Dynamic LINQ code like that shown here on SO within an Umbraco environment or indeed System.Linq.Dynamic (nuget link: https://www.nuget.org/packages/System.Linq.Dynamic/) ? And will it work within Umbraco 8?

    https://stackoverflow.com/questions/41244/dynamic-linq-orderby-on-ienumerablet

    I can see this question coming up a lot once Umb8 drops dynamics and we're all using strongly typed syntax.

  • Paul 184 posts 646 karma points
    Aug 09, 2018 @ 14:50
    Paul
    0

    Thread updated slightly. As the rewrite of this code is so it'll still work when dynamics are dropped in Umb 8, I've marked it as an Umbraco 8 query.

  • Paul 184 posts 646 karma points
    Aug 10, 2018 @ 11:42
    Paul
    0

    Ok for those having the same issue, the solution that appears to be working for me is to create the orderby statement within a function, then call the function in.

    E.g.

         IEnumerable<IPublishedContent> fileList;
    
        Func<IPublishedContent, Object> orderByFunc = null;
        if (sortbyoption == "Name")
        {
            orderByFunc = x => x.Name;
        }
    
    fileList = currFolder.Children.OrderBy(orderByFunc);
    

    So effectively I'm using a function as a variable.

    Hopefully this will help someone down the line!

Please Sign in or register to post replies

Write your reply to:

Draft