Copied to clipboard

Flag this post as spam?

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


  • Levente Kosa 136 posts 352 karma points
    May 01, 2020 @ 14:56
    Levente Kosa
    0

    foreach order by children

    Hi,

    I would like to know if it's possible to order by a children (or even grand-children) value, like:

    foreach (var item in items.Children.Where(x => x.IsVisible().OrderBy(x => x.Value("foo"))) {...}
    

    except "foo" value lives in a child node.

  • Ric Carey 50 posts 93 karma points
    May 01, 2020 @ 15:03
    Ric Carey
    1

    Hey Levente,

    Thats exactly how you do it, what you may be missing is the cast inside the loop try:

    items.Children.Where(x => x.IsVisible()).OrderBy(x => x.Value<string>("foo"))
    

    There was also a missing bracket to close your Where after IsVisible()

  • Levente Kosa 136 posts 352 karma points
    May 01, 2020 @ 15:06
    Levente Kosa
    0

    Thanks Ric for the quick answer. It was my bad, the looked value lives in a grand-child node. It's an integer btw.

    Is it possible with grand-child node somehow?

  • Ric Carey 50 posts 93 karma points
    May 01, 2020 @ 15:44
    Ric Carey
    0

    I'm not 100% sure what you are trying to achieve, it may be that you need to do some code to order the items before the foreach loop.

    But you could try

    items.Children.Where(x => x.IsVisible()).OrderBy(x => x.Children.Select(y => y.Value<int>("foo")))
    
  • Levente Kosa 136 posts 352 karma points
    May 01, 2020 @ 16:21
    Levente Kosa
    0

    Thank you.

    I tried, but it gave me this error:

    At least one object must implement IComparable.

    Anyway, I'll keep trying.

Please Sign in or register to post replies

Write your reply to:

Draft