Grouping in foreach and view does not work

Asked

Viewed 278 times

0

I created and set a variable to control the following. My LINQ returns 5,000 lines today. This variable, it’s there to avoid repeating the same thing, like. Let’s say Linus gives me 400 ID=1 motif. So this variable, I select it so that only Motive 1 only appears on the screen once. Well, that worked. What’s the problem then? For a single reason, I have 5 business units, for example. This UN can be repeated several times, as stated for the reason. I did similar to Reason and it didn’t work.

1) The IF I made is printed on the page. 2) Even if I have more than one UN for each Motive, only one UN is shown and nothing else and so on to the other levels below the UN. 3) I think the problem is in the Controller. Below is my controller(Actionresul) and my View(Only the part that mounts the Treeview.

Controller:

public ActionResult Acao()
        {
            RupturaEntities db = new RupturaEntities();

            var monta_arvore = db.Ruptura.Where(m => m.IDMotivo != 6)  
                               .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,
                                   Franquia = rup.Apresentacao.Franquia,
                                   Familia  = rup.Apresentacao.Familia,
                                   Descricao = rup.Apresentacao.Descricao
                               }).ToList().Take(50).OrderBy(r => r.IDMotivo);

            return View(monta_arvore.ToList());
        }

My View:

@{
                var _motivo = "";
                var _un = "";
                var _familia = "";
            <ul>
                @foreach (var item in Model)
                {
                    if (_motivo != @item.Motivo)
                    { 
                    <li item-checked='false' item-expanded='false'>
                        @item.Motivo
                        <ul>
                            <li item-checked='false' item-expanded='false'>
                                @item.Unidade_Negocio
                                <ul>
                                    <li item-checked='false' item-expanded='false'>
                                        @item.Familia
                                        <ul>
                                            <li item-checked='false' item-expanded='false'>
                                                @item.Descricao
                                            </li>
                                        </ul>
                                    </li>
                                </ul>

                            </li>
                        </ul>
                    </li>
                } 
                    _motivo = @item.Motivo;
                    _un = @item.Unidade_Negocio;
                    _familia = @item.Familia;
                }
            </ul>
           }

Guys, I put group by on LINQ and it didn’t work. It’s a mistake. I tried to do it in the view and there’s a problem with not recognizing some fields. Besides I did some IF’s to control and the next thing happens. The first IF that is about the Motive, works, IE, does not allow the repetition of the Motive. Already the others do not work and are printed on the screen. See how my View turned out with these IF’s.

<div id='jqxWidget'>
    <div style='float: left; width:auto;'>
        <div id='jqxTree' style='visibility: hidden; float: left; margin-left: 20px;'>

            @{
                var _motivo = "";
                var _un = "";
                var _familia = "";
            <ul>
                @foreach (var item in Model)
                {
                    if (_motivo != item.Motivo)
                    { 
                    <li item-checked='false' item-expanded='false'>
                        @item.Motivo
                        <ul>
                            if (_un != item.Unidade_Negocio)
                            { 
                            @foreach(var un in @item.Unidade_Negocio)
                            {
                            <li item-checked='false' item-expanded='false'>
                                @item.Unidade_Negocio
                                <ul>
                                    @foreach(var fam in @item.Familia)
                                    { 
                                    <li item-checked='false' item-expanded='false'>
                                        @item.Familia
                                        <ul>
                                            <li item-checked='false' item-expanded='false'>
                                                @item.Descricao
                                                <ul>
                                                    <li item-checked='false' item-expanded='false'>
                                                        @item.CnpjDescricao
                                                    </li>
                                                </ul>
                                            </li>
                                        </ul>
                                    </li>
                                    }
                                </ul>

                            </li>@*Unidade Negocio*@
                            }
                            }
                        </ul>
                    </li>
                } 
                    _motivo = @item.Motivo;
                    _un = @item.Unidade_Negocio;
                    _familia = @item.Familia;
                }
            </ul>
           }

        </div> 

Let me explain. In LINQ everything is loaded as template posted here.

Now, when I press the View, that’s where some things happen. If I put a Groupby in LINQ, as example posted, gives error right in the first record, which in the case is the Reason, but could be any other. Error already posted here. The query brings me many things repeated, like, Reason, UN and etc... Then I declared a variable and inside the foreach, it will be set with the value that it is controlling, like: Let’s say Reason. It has an empty value. Then I put an IF to know if the value of the variable is different from the reason. If it is (the first always is) and then I press the Reason. If in the next iteration, it remains the same, then jumps not repeating the same name. Well, as Reason is the first field, this is working well. When I get to the UN, it no longer works. However, writing this post, I think I put the var in the wrong place, IE, should be at the end of the UN foreach and not in the foreach of the Reason, as it is. But I will test, but anyway, it is not working the other IF’s. The first one works, but the others don’t. That’s what is getting in the way. I’m searching the net, but I haven’t seen anything really that solves my problem with IF’s nested inside a View(CSHTML). This is my headache.

  • You know the method GroupBy?

  • Yes, but I don’t think so. The problem is not in the result of Ingles, but in the assembly of treeview and the checkboxes. Of course it has to do with LINQ.

  • So why don’t you group the results by Reason and Business Unit? Leaving this to View gets complicated and slow.

1 answer

2

Already tried the Grupby with Distinct when mounting the List?

Something like:

public ActionResult Acao()
    {
        RupturaEntities db = new RupturaEntities();

        var monta_arvore = db.Ruptura.Where(m => m.IDMotivo != 6)  
                           .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,
                               Franquia = rup.Apresentacao.Franquia,
                               Familia  = rup.Apresentacao.Familia,
                               Descricao = rup.Apresentacao.Descricao
                           }).Distinct().ToList().Take(50).OrderBy(r => r.IDMotivo).ToList().GroupBy(x => x.IDMotivo).Select(x => x.First()).ToList();

        return View(monta_arvore.ToList());
    }
  • With Groupby gives the following arro:An Exception of type 'Microsoft.CSharp.Runtimebinder.Runtimebinderexception' occurred in System.Core.dll but was not handled in user code Additional information: 'Object' does not contain a definition for 'Motif'. You would have to put all LINQ fields in the grouping

Browser other questions tagged

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