Left Join with a table does not return null value

Asked

Viewed 145 times

3

I have this select:

var pesquisa = (from p in db.Produtos
                            join pe in db.ProdutosEmpresas on p.Id equals pe.ProdutoID into peph
                            from pe_ph in peph.DefaultIfEmpty()
                            select new
                            {
                                p.Codigo,
                                p.nome,

                            }).OrderBy(p => p.Codigo).ToList();

If I do so, it returns me the records correctly, only I need to bring the field EmpresaID table ProdutosEmpresas when I put to bring it returns error, because it can return null.

In the SQL would look this way:

select Produtos.Codigo, Produtos.nome, ProdutosEmpresas.EmpresaID from Produtos
left join ProdutosEmpresas on Produtos.id = ProdutosEmpresas.ProdutoID

It returns the following error:

Nullable Object must have a value.

I have to throw him on a list, so I need the value EmpresaID

List<Produto> prod = new List<Produto>();

            foreach (var item in pesquisa)
            {
                Produto produto = new Produto();

                produto.Codigo = item.Codigo;
                produto.nome = item.nome;
                produto.EmpresaID = item.EmpresaID;

                prod.Add(produto);
            }

Class Produto

 [Key]
    public int Id { get; set; }   
    public string Codigo { get; set; }
    public string nome { get; set; }

Class ProdutosEmpresas

 [Key]
    public int Id { get; set; }
    public Empresa EmpresaProduto { get; set; }
    public int EmpresaID { get; set; }
    public Produto ProdutoEmpresa { get; set; }
    public int ProdutoID { get; set; }
  • Hello @marianac_costa. How did you put the property EmpresaID on LINQ? Error because?

  • @Joãomartins updated the question.

  • Mariana, please put the Product class in the question to see what kind of data you are declaring.

  • @Angelosimonato ready, added the two classes

1 answer

4


Mariana, I’m going to put as an answer right now because I think this is the case.

The Class you are using is demanding values, that is, they are NOT NULL fields.

When you are bringing a null field in select because it is a left Join there is no possibility for the variable to receive a null value for this reason.

Example:

[Key]
    public int Id { get; set; }
    public Empresa EmpresaProduto { get; set; }
    public int? EmpresaID { get; set; }
    public Produto ProdutoEmpresa { get; set; }
    public int ProdutoID { get; set; }
  • I guess you didn’t quite understand what I’d like, it really has that power come null therefore the left join

  • I understand, but you are assigning in the code a null value in a variable that does not accept null. Integer cannot receive null, only if it is a nullable int ( which is not the case for the declaration of its class)

  • Oh yes, now I understand ,I thought I could do like one left join in the sql rs worked out here, thanks, now I get it.

  • Welcome, C# for being a strongly typed language forces you to be aware of this kind of question.

Browser other questions tagged

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