Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
I don't have a programming problem, but I'd like to know why.

I have something like this

List<MyClass> a,b,c<br /><br />var d = a.Except(b);<br />c.Clear()<br />c = d.ToList();


Of course, lists a & b are populated preceding the code. The question is, why can't I cast directly to List<myclass> as such:

<br />c = a.Except(b);<br />


Posted

You can't cast it as such because a.Except(b) returns the type System.Linq.Enumerable.ExceptIterator. You have two options here:
c.AddRange(a.Except(b));
or
c = a.Except(b).ToList();


 
Share this answer
 
Comments
Nish Nishant 27-Jul-10 9:52am    
Reason for my vote of 5
Great answer.
because they're basically different types. a.Except(b) does not return a list, it returns an "Enumerable" type.

so

IEnumerable c
List a,b

c = a.Except(b)

will work
 
Share this answer
 


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900