Left Join - Linq is bringing Duplicate Value

Asked

Viewed 55 times

0

Hello, I have a print method, which makes several left join, it is bringing the values I want, but some of these values are coming duplicated, as shown in the images:

Table TabelaImposto: select TbImp_codigo, TbImp_TpMdobc from TabelaImposto

inserir a descrição da imagem aqui

Table TipoModBaseICMS: select TpMdoBC_Codigo, TpMdoBC_Descricao from TipoModBaseICMS

inserir a descrição da imagem aqui

Result Obtained: inserir a descrição da imagem aqui

My method:

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

            var impostos = (from a in bdprincipalEntities.TabelaImposto

                            join e in bdprincipalEntities.TipoModBaseICMS on a.TbImp_TpMdobc
                            equals e.TpMdoBC_Codigo into a_e
                            from e in a_e.DefaultIfEmpty()

                            select new
                            {
                                a.TbImp_codigo,

                                e.TpMdoBC_Descricao

                            }).ToList();

            result.Content = new StringContent(JsonConvert.SerializeObject(impostos));
            result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            return result;
        }
        catch (Exception)
        {
            return new HttpResponseMessage(HttpStatusCode.BadRequest);
        }
    }
  • For the result in the image TbImp_Codigo is a different value just the description that hit, on the table how are the results? 'cause from the looks of it you’re right, like that’s what it’s to bring, it’s got to explain a few more details?

  • I will edit the question and try to give more detail

  • 1

    @Virgilionovic could tell me if you’re easier to understand or find the problem?

  • 1

    Just look at the table that the codes have no unique identity ... that’s why expensive!

1 answer

3


The field TpMdoBC_Codigo table TipoModBaseICMS is not only!

Let’s take for example, Code Tax 1. He has the TbImp_TpMdobc value 3. By doing the join with the table TipoModBaseICMS 2 records are found:

  • "Value of the Transaction"
  • "Neutral List (Value)"

The problem is not in the query, it is in the data structure.

Browser other questions tagged

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