Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nested linq queries, how to get distinct values?

table data of 2 columns "category" and "subcategory"

i want to get a collection of "category", [subcategories] using code below i get duplicates. Puting .Distinct() after outer "from" does not help much. What do i miss?

 var rootcategories = (from p in sr.products
                                 orderby p.category
                                  select new
                                  {
                                      category = p.category,
                                      subcategories = (
                                      from p2 in sr.products
                                      where p2.category == p.category
                                      select  p2.subcategory).Distinct()
                                  }).Distinct();

sr.products looks like this

category   subcategory
----------------------
cat1       subcat1
cat1       subcat2
cat2       subcat3
cat2       subcat3

what i get in results is

cat1, [subcat1,subcat2]
cat1, [subcat1,subcat2]

but i only want one entry

solved my problem with this code:

   var rootcategories2 = (from p in sr.products
                               group p.subcategory by p.category into subcats

                               select subcats);

now maybe it is time to think of what was the right question.. (-:

like image 627
Alexander Taran Avatar asked Nov 09 '08 17:11

Alexander Taran


4 Answers

solved with this code

   var rootcategories2 = (from p in sr.products
                               group p.subcategory by p.category into subcats

                               select subcats);

thanks everyone

like image 190
Alexander Taran Avatar answered Sep 25 '22 00:09

Alexander Taran


I think you need 2 "Distinct()" calls, one for the main categories and another for the subcategories.

This should work for you:

var mainCategories = (from p in products select p.category).Distinct();

var rootCategories =
    from c in mainCategories
    select new {
        category = c,
        subcategories = (from p in products
                         where p.category == c
                         select p.subcategory).Distinct()
    };
like image 40
chakrit Avatar answered Sep 25 '22 00:09

chakrit


The algorithm behind Distinct() needs a way to tell if 2 objects in the source IEnumerable are equal. The default method for that is to compare 2 objects by their reference and therefore its likely that no 2 objects are "equal" since you are creating them with the "new" keyword.

What you have to do is to write a custom class which implements IEnumerable and pass that to the Distinct() call.

like image 29
user35959 Avatar answered Sep 23 '22 00:09

user35959


Your main query is on Products, so you're going to get records for each product. Switch it around so you're querying on Category, but filtering on Product.Category

like image 30
GeekyMonkey Avatar answered Sep 25 '22 00:09

GeekyMonkey