Copied to clipboard

Flag this post as spam?

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


  • Paul Griffiths 370 posts 1021 karma points
    Feb 03, 2016 @ 09:11
    Paul Griffiths
    0

    Check to see if a list contains more than one specified value

    Hi All,

    I have a list called blogCategoriesList (which is of type string) that is populated with the value of each blog category as it loops through each page by checking the value defined in the category dropdown property. So if I had three blog posts on my site and the categories were defined as football, darts, darts then this is what the list would contain.

    I am trying to do an if check that verifies whether

    • the list contains a value that is the same category as the
      currentpage
    • the list contains more than one entry that is the same as the category of the current page

    Examples

    blogCategoriesList = football, darts, darts

    • if I was on a page that had the category set as darts then the if test should pass because the list contains darts and it also has more than one entry for darts in the list.

    blogCategoriesList = football, Computing, darts

    • if I was on a page that had the category set as darts then the if test would fail because the list contains darts but it doesn’t have more than one entry for darts in the list.

    I have been trying to work with the following code that uses groupby but struggling to get this to work at the moment.

    @if (blogCategoriesList.Contains(Model.Content.GetPropertyValue("category")) && blogCategoriesList.GroupBy(n => n.Equals(Model.Content.GetPropertyValue("category"))).Any(c => c.Count() > 1))
    

    Any help would be great

    Many thanks

    Paul

  • Paul Griffiths 370 posts 1021 karma points
    Feb 03, 2016 @ 18:03
    Paul Griffiths
    0

    Hi All,

    I resolved this issue with the following code

     @if (blogCategoriesList.Count(x => x.ToLower() == currentPageCategory.ToLower()) > 1)
    

    Realised i didnt need to use contains as if count was greater than 1 then the list obviously contains a value for the current page category

    Cheers Paul

Please Sign in or register to post replies

Write your reply to:

Draft