嵌套的linq查询,如何获得不同的值?

时间:2022-09-24 18:01:00

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

2列“类别”和“子类别”的表数据

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?

我想得到一个“类别”的集合,[subategories]使用下面的代码我得到重复。在“from”之外的Puting .Distinct()没有多大帮助。我错过了什么?

 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

sr.products看起来像这样

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

现在也许是时候想出什么是正确的问题..( - :

4 个解决方案

#1


5  

solved with this code

解决了这段代码

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

                               select subcats);

thanks everyone

#2


3  

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

我认为你需要2个“Distinct()”调用,一个用于主要类别,另一个用于子类别。

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()
    };

#3


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.

Distinct()背后的算法需要一种方法来判断源IEnumerable中的2个对象是否相等。默认方法是通过引用比较2个对象,因此可能没有2个对象“相等”,因为您使用“new”关键字创建它们。

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

你要做的是编写一个实现IEnumerable的自定义类,并将其传递给Distinct()调用。

#4


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

您的主要查询是产品,因此您将获得每个产品的记录。切换它,以便您查询类别,但过滤Product.Category

#1


5  

solved with this code

解决了这段代码

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

                               select subcats);

thanks everyone

#2


3  

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

我认为你需要2个“Distinct()”调用,一个用于主要类别,另一个用于子类别。

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()
    };

#3


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.

Distinct()背后的算法需要一种方法来判断源IEnumerable中的2个对象是否相等。默认方法是通过引用比较2个对象,因此可能没有2个对象“相等”,因为您使用“new”关键字创建它们。

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

你要做的是编写一个实现IEnumerable的自定义类,并将其传递给Distinct()调用。

#4


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

您的主要查询是产品,因此您将获得每个产品的记录。切换它,以便您查询类别,但过滤Product.Category