2
I have the following classes:
public class Pedidos_Itens
{
public int ID { get; set; }
public int Qtde { get; set; }
public Pedidos _Pedido { get; set; }
public int Pedido { get; set; }
public Loja_Produtos _Produto;
public int Produto { get; set; }
}
And
public class Loja_Produtos
{
public int ID { get; set; }
public string Nome { get; set; }
}
And
public class Pedidos
{
public int ID { get; set; }
public DateTime Data { get; set; }
public decimal Valortotal { get; set; }
}
But the bank doesn’t have a foreign key and I can’t modify the bank’s NADA. So I cannot use include (I believe it is because of the absence of the foreign key). So I did the following:
public IEnumerable<Pedidos_Itens> ListarTodos()
{
var itens = contexto.PedidoItem.ToList();
var list = new List<Pedidos_Itens>();
foreach (var i in itens)
{
i._Produto = contexto.Produto.First(x => x.ID == i.Produto);
i._Pedido = contexto.Pedido.First(x => x.ID == i.Pedido);
}
return list;
}
But I believe there’s a more efficient way to do the same.
The following error occurred: An unhandled Exception of type 'System.Notsupportedexception' occurred in Entityframework.SqlServer.dll Additional information: The Entity or Complex type 'Repositorioef.Pedidos_items' cannot be constructed in a LINQ to Entities query.
– Diego Zanardo
@Diegozanardo try to get the guy
Pedidos_Itens
that I am giving new, and make a Dynamic Object. If this works, the performance will be as optimized as.– JuninZe
I think the problem is that there are products within Pedidoitem, which no longer exist in the product table.
– Diego Zanardo
@Diegozanardo try then use
.FirstOrDefault
instead of.First
– JuninZe