Finding matching lists or sets

4 posts, 1 answers
  1. XXXX
    XXXX avatar
    57 posts
    Member since:
    Jun 2008

    Posted 29 Sep 2011 Link to this post

    This is probable Linq 101 question but here goes.
    Lets say we have 3 classes Book, TagCategory, Tag
    A book can have many Tags and Tag is only in one category.

    In the DTO part of the solution there are 3 classes Book, TagCategorySelected, Tag.
    Where TagCategorySelected holds what tags each book has in each TagCategory.
    So each book has List<TagCategorySelected>

    What I'm trying to figure out is how to get from a List<Book>  a book that has a maching List<TagCategorySeleced>
    as the user has selected. 
    In simple words (or mock linq)  List<Book>.Where( b => b.list<TagCategorySelected> == User.list<TagCategorySelected>)
    The TagCateforySelected has .Equals and '==' overloaded if that makes any difference.

    I just need a push in the right direction.
  2. XXXX
    XXXX avatar
    57 posts
    Member since:
    Jun 2008

    Posted 30 Sep 2011 Link to this post

    The only solution that I have come up with is to
    Select all the TagId's from the user selection into a IOrderedEnumerable and the same for each book then compare the lists useing SequenceEqual.

    Something similar to this:

    IOrderedEnumerable<int> userTags = User.List.SelectMany(s => 
    s.selectedtags).Select(i => i.TagId).Orderby(n => n);
    List<book> MatchingBooks = new List<book>();
    foreach (book candidate in AllBooks)
    {
       IOrderedEnumerable<int> bookTags = candidate.SelectMany(s => s.selectedtags).Select(i => i.TagId).Orderby(n => n);
        if(userTags.SequenceEqal(bookTags))
        {
            MatchingBooks.Add(book);
         }
    }
    I would have prefered something more elegant.

  3. DevCraft banner
  4. Answer
    Nikola
    Admin
    Nikola avatar
    27 posts

    Posted 04 Oct 2011 Link to this post

    Hi Bjössi,

    Please find attached sample project. I hope you will find this solution better.

    I included a Distinct() statement to eliminate the duplication of tags. I am also not sure about the way your example goes. Are you sure that you want to find only the books that contain exactly the same tags as all the books the user has selected?

    All the best,
    Nikola
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's SQL Server Community Awards. We are competing in TWO categories and every vote counts! VOTE for Telerik NOW >>

  5. XXXX
    XXXX avatar
    57 posts
    Member since:
    Jun 2008

    Posted 04 Oct 2011 Link to this post

    Thanks for a great suggestion for a solution.

    I should have been clearer on the scope of the requirement I'm attempting to solve.
    It has nothing to do with books, books were just something that I thought was eazy to understand.
    Distinct() was omitted since in the real project I'm working on the IList<SelectedTag> have been cleared of duplicates, but in such a general sample I should have included it.

    Thanks again
Back to Top