This is a migrated thread and some comments may be shown as answers.

Finding matching lists or sets

3 Answers 47 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
XXXX
Top achievements
Rank 1
XXXX asked on 29 Sep 2011, 02:18 PM
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.

3 Answers, 1 is accepted

Sort by
0
XXXX
Top achievements
Rank 1
answered on 30 Sep 2011, 11:31 AM
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.

0
Accepted
Nikola
Telerik team
answered on 04 Oct 2011, 01:42 PM
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 >>

0
XXXX
Top achievements
Rank 1
answered on 04 Oct 2011, 02:12 PM
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
Tags
LINQ (LINQ specific questions)
Asked by
XXXX
Top achievements
Rank 1
Answers by
XXXX
Top achievements
Rank 1
Nikola
Telerik team
Share this question
or