5

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.. (-:

Alexander Taran
  • 6,655
  • 2
  • 39
  • 60

4 Answers4

5

solved with this code

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

                               select subcats);

thanks everyone

Alexander Taran
  • 6,655
  • 2
  • 39
  • 60
3

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()
    };
chakrit
  • 61,017
  • 25
  • 133
  • 162
2

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.

user35959
  • 21
  • 1
1

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

GeekyMonkey
  • 12,478
  • 6
  • 33
  • 39