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
    Oct 08, 2019 @ 15:57
    Levente Kosa
    0

    Remove duplications form list

    Hi,

    I try to figure out how to remove duplications from this list, but I can't. I though I can use distinct like here:

    var plots = development.Children.Where(x => x.IsVisible());
    
    foreach (var plot in plots.Select(x => x.Value("bedrooms")).Distinct()) {
        <p>@plot</p>
    }
    

    Any idea how should I fix it?

  • Shaishav Karnani from digitallymedia.com 354 posts 1638 karma points
    Oct 09, 2019 @ 06:46
    Shaishav Karnani from digitallymedia.com
    0

    Hi,

    What type of datatype is bedrooms property. If it has been string or integer then it will allow but if it is iPublishedContent then it will not work.

    Please can you confirm.

    Regards,

    Shaishav

  • Levente Kosa 136 posts 352 karma points
    Oct 09, 2019 @ 07:09
    Levente Kosa
    0

    Hi,

    It's a numeric field.

    Thanks, Levente

  • Levente Kosa 136 posts 352 karma points
    Oct 09, 2019 @ 10:37
    Levente Kosa
    0

    So should it work with this code?

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Oct 29, 2019 @ 07:44
    Simon Dingley
    0

    @Levente you don't mention what the issue is? Do you get an error or do you just not get a list of unique values? If the value is numeric you can cast it as follows (not that it should rectify whatever issue you are having).

    foreach (var plot in plots.Select(x => x.Value<int>("bedrooms")).Distinct()) {
        <p>@plot</p>
    }
    
  • Levente Kosa 136 posts 352 karma points
    Oct 29, 2019 @ 10:12
    Levente Kosa
    0

    I just realised that around this foreach there is another foreach, so maybe that causes the issue, which is "the list values are not unique". There are duplications.

    So here is my full code, now added the <int> as well.

    var developments = Model.Root().Children.Where(x => x.IsDocumentType("developments")).FirstOrDefault();                           
    foreach (var development in developments.Children.Where(x => x.IsVisible())) {
        var plots = development.Children.Where(x => x.IsVisible() && x.Value<bool>("plotApartment"));
        foreach (var plot in plots.Select(x => x.Value<int>("bedrooms")).Distinct())
        {
            <p>@plot</p>
        }
    }
    
  • Simon Dingley 1474 posts 3431 karma points c-trib
    Oct 29, 2019 @ 10:20
    Simon Dingley
    0

    Sorry, early start this morning and overlooked the original code...

    You don't need a foreach to get a distinct list of bedrooms, just select them like this.

    var bedrooms = plots.Select(x => x.Value("bedrooms")).Distinct()
    
Please Sign in or register to post replies

Write your reply to:

Draft