Problems with logic to bring data from a list or table

4

I made several posts here and solved almost every problem. 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 there's something missing in me to solve the issue.

I have linq that brings me everything I need. But as it comes, I can not mount my screen.

There, after several posts and guidelines here and on another site, I was told that I export separate lists for each intended situation. So I lost everything and I ask you for help.

My scenario is this:

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 made the second linq to fetch the UN for each Reason, it was there that I realized I was wrong . There is something missing that I can not fit into.

I was able to make the Motives and UNs not repeat. But I can not list the UN for the corresponding Reason.

In linq UN, I took it from the Presentation table and this table does not relate to the reason and so the problem.

Below the linq :

The first is everything and the second attempt to bring the UN for each reason. It did not work.

My linq (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 UN linq:

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 what really does not work, the way it is.

    
asked by anonymous 22.09.2014 / 21:15

1 answer

1

I swear that I did not understand your question very much, but I will respond to try to help you ... (I'm not so familiar with Linq)

List<UN> ListaUN;//carrega a lista de UN do banco.

foreach (var _idmotivo in monta_arvore)
{
    var listaUnidade = ListaUN.Where(x => x.IDMotivo == _idmotivo.IDMotivo);
    //seria isso?
}

If it is an internal list of it (which I believe is your case), you can do so too:

Apresentacao.Where(x => x.ListaUN.All(s => s.IDMotivo == _udmotivo.IDMotivo)));

Please leave a comment!

    
25.09.2014 / 03:13