Linq brings result but does not obey Join

Asked

Viewed 54 times

1

We’re getting closer. With this code, it almost worked. Then I switched the Distinct() for DistinctBy() and did not repeat, but still not obeying the IDMotivo. What comes in Linnum does not match with the query. In all the Reasons I brought 3 UN’s, whereas in the Motive of ID=4, I should have just 2 UN’s, which seems to be bringing the same information, regardless of the IDMotivo.

I’m here trying a solution. See how my code:

public static List<MontaArvoreAcao> CriarListaArvoreAcao()
{
    RupturaEntities db = new RupturaEntities();

    var _listaUnidade = (
        from r in db.Ruptura
        join a in db.Apresentacao on r.Codigo_Apresentacao equals (a.Codigo_Apresentacao)
        join m in db.Motivo on r.IDMotivo equals (m.IDMotivo)
        where r.IDMotivo != 6
        group r by new { a.Unidade_Negocio, r.IDMotivo } into gr
        select new MontaArvoreAcao
        {
            IDMotivo = gr.Key.IDMotivo,
            Unidade_Negocio = gr.Key.Unidade_Negocio
        }).DistinctBy(u => u.Unidade_Negocio)
          .DistinctBy(m => m.IDMotivo)
          .ToList().OrderBy(r => r.IDMotivo);

    return _listaUnidade.ToList();
}

The way it is, without the DistinctBy, I can bring all the UN’s. Bringing a reading of UN’s thus:

{GENÉRICOS,MIP,DERMOCOSMÉTICOS},
{DERMOCOSMÉTICOS,MIP,GENÉRICOS},
{MIP,GENÉRICOS,DERMOCOSMÉTICOS},
{DERMOCOSMÉTICOS,GENÉRICOS},
{MIP,GENÉRICOS,DERMOCOSMÉTICOS}

For their Reasons:

{VENDEU TODO ESTOQUE},
{PRODUTO EM FALTA NO CENTRO DE DISTRIBUIÇÃO},
{PRODUTO NÃO CADASTRADO NA CENTRAL},
{PRODUTO INATIVO},
{PRODUTO CADASTRADO / SOB ENCOMENDA}

Representing, respectively, the ID’s: 1,2,3 and 4.
See that brought me according to query, but I can’t make a redistribution for every Reason.

How is my screen:

My canvas creates the first knot with the checkbox for each Motivo. The reasons are listed correctly and the checkbox are created correctly.

So when I go to list the UN’s, each one below its corresponding motif, is that it doesn’t work properly, that is, for each Motif created on the page, I list all these Motifs at once for each Motif.

The correct is to list, for each Motivo, as UN's correspondents. May be missing something, some ID, but which?

I got to a point where I didn’t know any more. It should be a Distinct for a ID appropriate, but how do I do it? That’s my cruel doubt that I’m going through.

But in this way I brought them all right. What is missing is the correct distribution for each Motivo correspondent. On hold and thank you

1 answer

1

Solved. I made the differentiation in the View itself and now it worked. Look how it was:

<ul>
    @foreach (var item in Model)
    {
        if (_motivo != @item.Motivo)
        {
            _idmotivo = @item.IDMotivo;
            <li item-checked='false' item-expanded='false'>
                @item.Motivo
                <ul>
                    @foreach(var un in (List<Ruptura.Models.MontaArvoreAcao>) ViewData["ListaUn"])
                    {
                        if (@un.IDMotivo == @item.IDMotivo)==> ***aqui resolveu***
                        { 
                            <li item-checked='false' item-expanded='false'>
                                @un.Unidade_Negocio
                            </li>
                        }
                    }
                </ul>
            </li>
        }
        _motivo = @item.Motivo;
    }
</ul>

Browser other questions tagged

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