The 'Distinct' operation cannot be applied to the collection ResultType of the specified argument

c# entity-framework linq linq-to-sql sql

Question

I wonder why I am not able to find this error on stackoverflow. In Linq to SQL, i am selecting an anonymous object like

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

In selecting anonymous type object above, in one of the properties I am selecting another list. I guess this might be causing the issue. I wonder if there is a workaround.

1
3
12/12/2013 10:11:25 AM

Accepted Answer

It may be because your query is returning more than one result,

try using

something = something.Distinct().ToList();

You can use a workaround if applicable to your requirements .

something = something.GroupBy(x => x.PropertyToCompare).Select(x => x.First());
10
12/12/2013 10:49:29 AM

Popular Answer

there is no way to Distinct() an anonymous type as each object will be held in different memory space, and therefore not equatable.

You will likely need to implement IEquatable to use Distinct() as per this response: Distinct not working with LINQ to Objects



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow