Error in left Join Linq - Dbcomparisonexpression requires Arguments with comparable types

Asked

Viewed 72 times

2

I have the following method:

However, when I run the query in the Log, using the left Join I get the following message :

Dbcomparisonexpression requires Arguments with comparable types

 [HttpGet]
    [Route("findAll")]
    public HttpResponseMessage findAll()
    {
        try
        {
            var result = new HttpResponseMessage(HttpStatusCode.OK);

            var tamanho = (from bc in bdprincipalEntities.tamanho
                           join c in bdprincipalEntities.fornecedor on bc.for_codigo
                           equals c.For_codigo into c_c
                           from c in c_c.DefaultIfEmpty()
                           join d in bdprincipalEntities.GrupoProduto on bc.GrupoProduto_Codigo
                           equals d.GrupoProduto_Codigo into d_bc
                           from d in d_bc.DefaultIfEmpty()
                           select new
                           {
                               Tam_codigo = bc.Tam_codigo,
                               Tam_descricao = bc.Tam_descricao,
                               GrupoProduto_Descricao = d.GrupoProduto_Descricao,
                               For_Nome = c.For_Nome,
                               for_codigo = bc.for_codigo,
                               tam_CodFor = bc.tam_CodFor,
                               Emp_codigo = bc.Emp_codigo,
                               Tam_situacao = bc.Tam_situacao.Equals("A") ? "ATIVO" : "DESATIVADO"

                          }).ToList();

            result.Content = new StringContent(JsonConvert.SerializeObject(tamanho));
            result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            return result;
        }
        catch (Exception ex)
        {
            return new HttpResponseMessage(HttpStatusCode.BadRequest);
        }
    }

The fields I am comparing are of type int:

public Nullable<int> for_codigo { get; set; } //Da tabela tamanho
public int For_codigo { get; set; } // Da tabela fornecedor

public Nullable<int> GrupoProduto_Codigo { get; set; } // Da tabela Tamanho
public int GrupoProduto_Codigo { get; set; } // Da tabela Grupo de Produto

1 answer

3


I believe the cause of your problem query is in the properties that are Nullable types.

A int nullable may receive a int, but in the passage from its select new {} you’re doing the reverse, passing a nullable for a int type and this is not right.

Note that bc.for_codigo represents:

public public Nullable<int> for_codigo { get; set; } //Da tabela tamanho

You’re attributing to this nullable for_cordigo which is a int type common, it will be necessary to adjust the query to invert or change the properties if possible.

You also need to evaluate the section below where the join, because the same error mentioned above may be occurring:

join c in bdprincipalEntities.fornecedor on bc.for_codigo
equals c.For_codigo into c_c

See this definition for Nullables types:

Types that allow null value have the following characteristics: types allowing null value represent variables of value type which can be assigned to the null value. You cannot create a type which allows null value based on a reference type. (The types of reference already support null value.)

If you want to know more see the complete guide here: Types that allow null value (Programming Guide in C#)

  • I understood, but even changing the properties happens the same error

  • Strange...I suggest you make one query isolated only with the left join who’s in trouble. , playing the result on a new list, to isolate and be able to smooth better, sometimes it may be up to something else that is causing the error.

  • I’ll do, just finish one thing, but I was analyzing, the names aren’t exactly the same, could that be? for_code and for_code

Browser other questions tagged

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