1
I have this SELECT, where m.EmpresaID may come null:
var pesquisa = from pro in db.Produtos
join prodempresa in db.ProdutosEmpresas on pro.Id equals prodempresa.ProdutoID
into Details from m in Details.DefaultIfEmpty()
select new
{
Codigo = pro.Codigo,
nome = pro.nome,
EmpresaID = m.EmpresaID.Value,
TipoProduto = pro.TipoProduto,
Qtd = m.Qtd
};
But when will I add in list, even though he was declared
public int? EmpresaID { get; set; }
it returns this error.
Invalidoperationexception: Nullable Object must have a value.
foreach (var item in pesquisa)
{
Estoque produto = new Estoque();
produto.Codigo = item.Codigo;
produto.nome = item.nome;
produto.EmpresaID = item.EmpresaID;
produto.TipoProduto = item.TipoProduto;
produto.Qtd = item.Qtd;
prod.Add(produto);
}
I tried to compare the value, send EmpresaID = m.EmpresaID.GetValueOrDefault(), but the same error occurs.
Ever tried to assign some value to
EmpresaID, even if it is null?– Felipe Avelar
@Felipeavelar already yes. And the same error occurred
– Mariana
If you take that line it works
EmpresaID = m.EmpresaID.Value,? perhaps themis already comingNULL?– novic