"Distinct"操作不能应用于指定参数的集合ResultType

Tah*_*qui 3 c# sql linq entity-framework linq-to-sql

我想知道为什么我无法在stackoverflow上找到此错误.在Linq to SQL中,我选择了一个匿名对象

var something = from a in .....
                ......
                ......
               select new { 
                                  myParameter = a.Something 
                                  myListParameter = (from b in ........
                                                    select b)
                          }
.
.
.
.
something = something.Distinct(); //This is giving error
Run Code Online (Sandbox Code Playgroud)

在选择上面的匿名类型对象时,我在其中一个属性中选择另一个列表.我想这可能会导致这个问题.我想知道是否有解决方法.

Sur*_*ngh 10

这可能是因为您的查询返回了多个结果,

尝试使用

something = something.Distinct().ToList();
Run Code Online (Sandbox Code Playgroud)

您可以根据需要使用变通方法.

something = something.GroupBy(x => x.PropertyToCompare).Select(x => x.First());
Run Code Online (Sandbox Code Playgroud)