4
I did several posts here and solved almost all problems. However I understood that the problem was in the logic that I set up to bring the result. I know I’m asking a lot, but I’m trying and I realize I’m missing something else in me to solve the question.
I have a linq that brings me everything I need. However the way it comes, I can’t assemble my canvas. 
There, after several posts and guidelines here and on another site, I was told to export separate lists for each desired situation. Then I got all lost and asked for help from you.
This is my scenario:
The first linq, brings me all the information from my bank, following some criteria. 
For each Reason, I bring a list of UN. Each UN a Family List and so on.
When I did the second linq to search the UN for each Reason, that’s when I realized I was wrong. Something’s missing that I can’t fit. 
I was able to make sure the Motives and the UN didn’t repeat themselves. But I cannot list the UN referring to the corresponding Reason.
In the linq UN, I took the table Presentation and this table does not relate to the Reason and therefore the problem. 
Down with the linq: 
The first behind everything and the second attempt to bring the UN concerning each Reason. It did not work.
My Breath(all):
var monta_arvore = db.Ruptura
       .Where(m => m.IDMotivo != 7)
       .Select(rup => new MontaArvoreAcao
       {
           IDRuptura = rup.IDRuptura,
           DataRuptura = rup.DataRuptura,
           IDMotivo = rup.IDMotivo,
           Motivo = rup.Motivo.Motivo1,
           IDOrigem = rup.IDOrigem,
           CodigoPDV = rup.CodigoPDV,
           UF = rup.PDV.UF,
           Cidade = rup.PDV.Cidade,
           CnpjDescricao = rup.PDV.Cnpj + " - " + rup.PDV.Descricao,
           Codigo_Apresentacao = rup.Codigo_Apresentacao,
           Unidade_Negocio = rup.Apresentacao.Unidade_Negocio,
           Codigo_Unidade_Negocio = rup.Apresentacao.Codigo_Unidade_Negocio,
           Franquia = rup.Apresentacao.Franquia,
           Familia = rup.Apresentacao.Familia,
           Descricao = rup.Apresentacao.Descricao
       }).ToList().OrderBy(r => r.IDMotivo);
My Linq UN:
foreach (var _idmotivo in monta_arvore)
{
    _listaUnidade = db.Apresentacao
        .GroupBy(g => new { Codigo = g.Codigo_Unidade_Negocio, UN = g.Unidade_Negocio})
        .Where(un => un.Key.Codigo != "0")
        .Select(u => new MontaArvoreAcao
        {
            Unidade_Negocio = u.Select(s => s.Unidade_Negocio).FirstOrDefault(),
            Codigo_Unidade_Negocio = u.Key.Codigo
        })
        .ToList()
        .OrderBy(o => o.Unidade_Negocio);
}
See that it really doesn’t work, the way it is.
I think you could structure your text better because it is very confusing. Use markers, break lines.
– Diego Zanardo
A smaller text, perhaps I can not pass my doubt, give as much subsidy as possible to the solution.
– pnet
I think I should walk the tree and relate the UN, right? I’ll go this way and see what happens.
– pnet
Want an infallible tip? query in SQL... test... then "translate" into Linqpad. Use the Linqpad program that will help you too.
– Danilo Breda