Use of the Ienumerable

Asked

Viewed 608 times

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 as List<Funcionario> or "var"). The method Where already has as return IEnumerable<TSource> (in the case, Ienumerable<Funcionario>). What this line does, is to convert a IEnumerable<Funcionario> for ToList<Funcionario> and then convert again to IEnumerable<Funcionario>.

  • 3

    @Andrefigueiredo If you remove the ToList, the code is functionally different. The first conversation of IEnumerable for List produces functional effects (eg, materializes a late-bound query), while the second conversation is a Reference Conversion, or upcast. If the ToList should 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.

  • Yeah. But my point is, the ToList will execute the query and then assign it to the same type IEnumerable again instead of just leaving it as ToList. If the expected result is to perform the query itself, why type its result as IEnumerable?

  • Yes, I agree, if the goal is even to call the ToList, then the local variable must be of the List<T>, there are no reasons otherwise

  • From what I understand it could be like this: var temp = functio. Where(x => x.Funcid == 2). Tolist<Funcio>();?

  • @Jotajotajota could also. In this case it would be more suitable to what Andre and dcastro are talking about. In this case temp be the type List<Funcionario> which is the type of return of the method ToList.

Show 1 more comment

2 answers

6


No. He’s the class builder EntFuncionarios(). And it is the entity that represents employees. It has connection with the class Funcionario, obvious. It is required for use with the standard of Entity Framework.

It is generated through the file .edmx existing in the solution. At least when using the templates Model Fisrt or Database First.

Ienumerable

The IEnumerable there is being used for another reason. It represents a concrete type that has an ability to pick up item by item from a collection. In this case (can not talk much without knowing all the details) it allows to sweep all employees contained in EntFuncionarios. This is done on the last line through a call query technique LINQ.

  • Thank you for the answer, would be what is mapped in the . edmx?

  • 1

    I don’t use EF so I can’t make you accurately, but it is common for this class to be created from the template that is set in this file yes. Depending on how you are using EF. I believe in this case it is Model Fisrt. correct me if I’m wrong. http://msdn.microsoft.com/en-us/data/jj650889.aspx

  • Thank you! It helped a lot!

  • 1

    Database First also.

  • 1

    @Andrefigueiredo yes, of course. Certainly not Code First :) In this case write the class in hand to define the template and create the database.

  • I’m going to do this. It’s because I tried to vote and I was liberated.

Show 1 more comment

5

It means that the variable ent has a data instance of the type EntFuncionarios, already defined in the database.

In other words, it’s like a new data row in the table EntFuncionarios, where all values are null, ready to be instantiated.

  • Thank you for your attention.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.