While there is a necessity of switching from one LINQ driver to another (e.g. because some wanted expressions can be unsupported in the first one) what kind of switch should be used - the AsEnumerable
or ToList
?
var r = ent.Users.Select(user => new
{
user.Name,
Organs = user.Attributes.Select(x => x.Organ)
})
.AsEnumerable() // switch to LINQ to Objects
.Select(user => new
{
user.Name,
Organs = string.Join(", ", user.Organs)
});
I understand that AsEnumerable
is deferred, so it doesn't enumerate the source immediately, but is there any significant difference from usage of ToList
as the switch here, in practice, instead ?
For LINQ to Objects to run here, the data from SQL should be already available - so it is what ToList()
will do if used in the place of the switch. Isn't the usage of AsEnumerable
force something like ToList
to be invoked internally anyway while evaluation of the expression tree ?