Why can't this cast be done?
-
I don't have a programming problem, but I'd like to know why. I have something like this
List a,b,c
var d = a.Except(b);
c.Clear()
c = d.ToList();Of course, lists a & b are populated preceding the code. The question is, why can't I cast directly to List as such:
c = a.Except(b);
If the post was helpful, please vote, eh! Current activities: Book: Devils by Fyodor Dostoyevsky Project: Hospital Automation, final stage Learning: Image analysis, LINQ Now and forever, defiant to the end. What is Multiple Sclerosis[^]?
-
I don't have a programming problem, but I'd like to know why. I have something like this
List a,b,c
var d = a.Except(b);
c.Clear()
c = d.ToList();Of course, lists a & b are populated preceding the code. The question is, why can't I cast directly to List as such:
c = a.Except(b);
If the post was helpful, please vote, eh! Current activities: Book: Devils by Fyodor Dostoyevsky Project: Hospital Automation, final stage Learning: Image analysis, LINQ Now and forever, defiant to the end. What is Multiple Sclerosis[^]?
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();
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
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();
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
I ended up doing the second one, but it seems counter intuitive (to me at least) not to be able to do it directly. Meaning that the cast should happen behind the scenes.
If the post was helpful, please vote, eh! Current activities: Book: Devils by Fyodor Dostoyevsky Project: Hospital Automation, final stage Learning: Image analysis, LINQ Now and forever, defiant to the end. What is Multiple Sclerosis[^]?
-
I don't have a programming problem, but I'd like to know why. I have something like this
List a,b,c
var d = a.Except(b);
c.Clear()
c = d.ToList();Of course, lists a & b are populated preceding the code. The question is, why can't I cast directly to List as such:
c = a.Except(b);
If the post was helpful, please vote, eh! Current activities: Book: Devils by Fyodor Dostoyevsky Project: Hospital Automation, final stage Learning: Image analysis, LINQ Now and forever, defiant to the end. What is Multiple Sclerosis[^]?
Except
does not return a list, but an enumerator that will calculate the results on demand. This is how all LINQ methods work (well, those that return IEnumerable): the result isn't calculated until it is used. It would be strange to have an implicit cast that has a side effect (evaluating the query).