C# 嵌套的 linq 查询,如何获取不同的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/276166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 21:10:17  来源:igfitidea点击:

nested linq queries, how to get distinct values?

c#linqlinq-to-sql

提问by Alexander Taran

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?

我想使用下面的代码获取“类别”的集合,[子类别] 我得到重复。将 .Distinct() 放在外部“from”之后并没有多大帮助。我想念什么?

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

现在也许是时候考虑什么是正确的问题了.. (-:

采纳答案by Alexander Taran

solved with this code

用这段代码解决了

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

                               select subcats);

thanks everyone

谢谢大家

回答by chakrit

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

回答by GeekyMonkey

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

您的主要查询是关于产品,因此您将获得每个产品的记录。切换它,以便您在 Category 上查询,但在 Product.Category 上进行过滤

回答by user35959

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() 调用。