2
I saw an example in another post about the use of IEnumerable
var ent = new EntFuncionarios();
IEnumerable<Funcionario> funcionario = ent.Funcionarios;
IEnumerable<Funcionario> temp = funcionario.Where(x => x.FuncID == 2).ToList<Funcionario>();
My question is about line var ent = new EntFuncionarios(), is a method of the Employees class?
It should be said that the
ToList<Funcionario>()should be removed (or the variable should be typed asList<Funcionario>or "var"). The methodWherealready has as returnIEnumerable<TSource>(in the case, Ienumerable<Funcionario>). What this line does, is to convert aIEnumerable<Funcionario>forToList<Funcionario>and then convert again toIEnumerable<Funcionario>.– Andre Figueiredo
@Andrefigueiredo If you remove the
ToList, the code is functionally different. The first conversation ofIEnumerableforListproduces functional effects (eg, materializes a late-bound query), while the second conversation is a Reference Conversion, or upcast. If theToListshould be removed or not, no one can answer, because it may or may not be necessary for the specific use case, of which we know nothing.– dcastro
Yeah. But my point is, the
ToListwill execute the query and then assign it to the same typeIEnumerableagain instead of just leaving it asToList. If the expected result is to perform the query itself, why type its result asIEnumerable?– Andre Figueiredo
Yes, I agree, if the goal is even to call the
ToList, then the local variable must be of theList<T>, there are no reasons otherwise– dcastro
From what I understand it could be like this: var temp = functio. Where(x => x.Funcid == 2). Tolist<Funcio>();?
– Jothaz
@Jotajotajota could also. In this case it would be more suitable to what Andre and dcastro are talking about. In this case
tempbe the typeList<Funcionario>which is the type of return of the methodToList.– Maniero