Reputation: 2937
I have this method on my DAL class:
public IEnumerable<Pedidos> Pedidos_Listar()
{
using (var context = new OhmioEntities())
{
var _ped =
from Pedidos in context.Pedidos
where Pedidos.ID_Cliente == 1
select new {Pedidos.ID_Pedido, Pedidos.Fecha, Pedidos.Clientes};
return _ped.ToList();
}
}
And VS give this error:
Error 3 No se puede convertir implícitamente el
tipo 'System.Collections.Generic.List<AnonymousType#1>'
en 'System.Collections.Generic.IEnumerable<Ohmio.ModelLayer.Pedidos>'.
Ya existe una conversión explícita (compruebe si le falta una conversión)
What i'm I doing wrong here?Thanks
EDIT More info: Pedidos is defined as POCOs db class So This work just fine:
public IEnumerable<Pedidos> Pedidos_Listar()
{
using (var context = new OhmioEntities())
{
return context.Pedidos.ToList();
}
}
But i need to use linq to select just some field and a where clause. When I try this as suggested:
public IEnumerable<Pedidos> Pedidos_Listar()
{
using (var context = new OhmioEntities())
{
var _ped =
from Pedidos in context.Pedidos
where Pedidos.ID_Cliente == 1
select new Pedidos {Pedidos.ID_Pedido, Pedidos.Fecha, Pedidos.Clientes};
return _ped.ToList();
}
}
I get this error:
Can't inicialize type 'Ohmio.ModelLayer.Pedidos' with a colection inicializer because
don't implement 'System.Collections.IEnumerable' (Manual translate)
I'm i missing something here? Thanks!
Upvotes: 1
Views: 2407
Reputation: 1288
Why do you want to instanciate new Pedidos? Try simply this:
public IEnumerable<Pedidos> Pedidos_Listar()
{
using (var context = new OhmioEntities())
{
return context.Pedidos
.Where(p => p.ID_Cliente == 1)
.AsEnumerable();
}
}
Upvotes: 0
Reputation: 89285
Cause of the original error you got already explained by @Selman22. But the solution causes another error because you can't create mapped entity (your POCOs db class) in LINQ-to-entity query. To workaround this, try to initialize the entity in client side (outside LINQ-to-entity query) :
public IEnumerable<Pedidos> Pedidos_Listar()
{
using (var context = new OhmioEntities())
{
var _ped =
(
from Pedidos in context.Pedidos
where Pedidos.ID_Cliente == 1
select new {Pedidos.ID_Pedido, Pedidos.Fecha, Pedidos.Clientes}
).ToList();
return _ped.Select(o => new Pedidos
{
ID_Pedido = p.ID_Pedido,
Fecha = p.Fecha,
Clientes = p.Clientes
});
}
}
Upvotes: 0
Reputation: 12805
You're attempting to return an collection of anonymous objects.
You'll need to name your object in your select
to Pedidos
:
select new Pedidos { ID_Pedido = Pedidos.Id_Pedido,
Fecha = Pedidos.Fecha,
Clientes = Pedidos.Clientes };
Upvotes: 0
Reputation: 101681
You are creating a list of anonymous objects.If you want a strongly-typed collection change your code like this:
var _ped = from p in context.Pedidos
where p.ID_Cliente == 1
select new Pedidos
{
ID_Pedido = p.ID_Pedido,
Fecha = p.Fecha,
Clientes = p.Clientes
};
Upvotes: 1
Reputation: 25351
You are creating a list of anonymous
objects and then trying to return it as list of Pedidos
. the compiler doesn't know how to do the casting. You can either change it to:
public IEnumerable<Pedidos> Pedidos_Listar()
{
using (var context = new OhmioEntities())
{
var _ped =
from Pedidos in context.Pedidos
where Pedidos.ID_Cliente == 1
select new Pedidos;
return _ped.ToList();
}
}
Or you will have to do the maping/casting yourself. Something like this will work:
public IEnumerable<Pedidos> Pedidos_Listar()
{
using (var context = new OhmioEntities())
{
var _ped =
from P in context.Pedidos
where P.ID_Cliente == 1
select new Pedidos { ID_Pedido = P.ID_Pedido,
Fecha = P.Fecha,
Clientes = P.Clientes};
return _ped.ToList();
}
}
Upvotes: 0
Reputation: 10398
You're not returning the type you said you would. You're returning an anonymous type. Fix this by projecting into the known type:
public IEnumerable<Pedidos> Pedidos_Listar()
{
using (var context = new OhmioEntities())
{
var _ped =
from Pedidos in context.Pedidos
where Pedidos.ID_Cliente == 1
select new Pedidos {Id = Pedidos.ID_Pedido, Fecha = Pedidos.Fecha, Clientes = Pedidos.Clientes};
return _ped.ToList();
}
}
Or if you don't need the projection and are returning the EF model directly:
public IEnumerable<Pedidos> Pedidos_Listar()
{
using (var context = new OhmioEntities())
{
var _ped =
from Pedidos in context.Pedidos
where Pedidos.ID_Cliente == 1
select Pedidos;
return _ped.ToList();
}
}
Or simply:
public IEnumerable<Pedidos> Pedidos_Listar()
{
using (var context = new OhmioEntities())
{
return context.Pedidos.Where(p => p.ID_Cliente == 1);
}
}
Upvotes: 1
Reputation: 7961
Your select
is selecting a new anonymous type, but you're trying to return that anonymous type as instances of Pedidos
. You need to change your select
to create instances of Pedidos
, instead.
Upvotes: 0
Reputation: 26644
I think you are looking for this:
select Pedidos;
Instead of this:
select new {Pedidos.ID_Pedido, Pedidos.Fecha, Pedidos.Clientes};
If this is Linq-to-entites, you can't project to an entity class, so this wouldn't work:
select new Pedidos { ... }
However, you can project to a new class like
select new PedidosCustomClass { ... }
Upvotes: 0