I am trying to change from IlistEmployee to listEmployee

0

I'm trying to move from Ilist to list and then display it in a DataGridView but I get an error ... it tells me that it is not possible to implicitly convert

List<Empleado> lista = new List<Empleado>();
lista = servicio.Consultar();
    
asked by Deiby Peralta 27.11.2017 в 02:34
source

3 answers

1

Good morning, The issue is that IList < > is an Interface, instead List < > is a class, IList exposes public methods that the List class implements. To solve your problem, you have three options, make the method return a List, or that the variable where you receive it is IList, or pass it the Interface in the constructor

IList<Empleado> lista;
lista = servicio.Consultar();

List<Empleado> lista = new List<Empleado>(servicio.Consultar());

Atte

    
answered by 27.11.2017 / 12:12
source
4

Use the extender method ToList() about the type IList<Empleado> :

List<Empleado> empleados = servicio.Consultar().ToList();

IList<T> implement IEnumerable<T> so you have available all the extensor methods of System.Linq.Enumerable , among them is the List<T> ToList(this IEnumerable<T> source) method that converts a IEnumerable<T> to List<T> .

    
answered by 27.11.2017 в 13:38
1

What you must do is have the new list created by passing to the constructor the IList that your method returns servicio.Consultar() :

List<Empleado> lista = new List<Empleado>(servicio.Consultar());
    
answered by 27.11.2017 в 12:16