count records in a table

1

Good I need to show a count I am working with a listing and lambda as follows:

@foreach (var item in Denuncias)
                            {
                                var DenunciaPersona = dbDP.TraerTodo().Where(x => x.NODENUNCIA == item.NODENUNCIA);
                                var hecho = dbhe.TraerHechos((long)item.AÑO, (long)item.NODENUNCIA).Where(x => x.NODENUNCIA == item.NODENUNCIA).FirstOrDefault();
                                var citaciones = dbci.TraerCitaciones((long)item.AÑO, (long)item.NODENUNCIA);
                                var clasedenuncia = dbcla.TraerTodo().Where(x => x.CLASE_DENUNCIA == item.CLASE_DENUNCIA);
                                DateTime? ultimaCitacion = null;
                                  <tr>                              
                                  <td nowrap>                                            
                                        @foreach (var d in DenunciaPersona)
                                        {
                                            int prueba = DenunciaPersona.Count(den => den.TIPO_PERSONA == 1);
                                            if (d.TIPO_PERSONA == 1)
                                            {

                                                    <span> cantidad de victimas:@prueba</span>

                                            }
                                        }
                                        <br />
                                        Clase Denuncia: @denuncia
                                    </td>

                                   </tr>

If you are telling me this, but show it to me in the following manner: VICTIMS QUANTITY: 2 VICTIMS QUANTITY: 2 And I want it just like this: VICTIMS QUANTITY 2

    
asked by rmonroy 18.12.2017 в 22:31
source

1 answer

2

As you say sstan in your comment , the @foreach (var d in DenunciaPersona) are not using it and not it is required, that is why it shows you the duplicated information, since for each DenunciaPersona you are printing the information on the screen. Simply remove that segment of code and it will work according to what you need.

The validation if (d.TIPO_PERSONA == 1) is also not necessary since you are including it in the Count(den => den.TIPO_PERSONA == 1); that you are doing. The code would look something like this:

@foreach (var item in Denuncias)
{
    var DenunciaPersona = dbDP.TraerTodo().Where(x => x.NODENUNCIA == item.NODENUNCIA);
    var hecho = dbhe.TraerHechos((long)item.AÑO, (long)item.NODENUNCIA).Where(x => x.NODENUNCIA == item.NODENUNCIA).FirstOrDefault();
    var citaciones = dbci.TraerCitaciones((long)item.AÑO, (long)item.NODENUNCIA);
    var clasedenuncia = dbcla.TraerTodo().Where(x => x.CLASE_DENUNCIA == item.CLASE_DENUNCIA);
    DateTime? ultimaCitacion = null;
    <tr>                              
        <td nowrap>
            int prueba = DenunciaPersona.Count(den => den.TIPO_PERSONA == 1);
            <span> cantidad de victimas:@prueba</span>
            <br />
            Clase Denuncia: @denuncia // --> Esto te va a marcar un error o simplemente verás en el html tal cual está: Clase Denuncia: @denuncia
        </td>
    </tr>
}

As an additional observation, I would recommend that you do not access the data model directly from a View, it is not the best practice and it becomes a difficult code to maintain in the future. Ideally, that information is obtained from a Controller and send the necessary information to the View.

    
answered by 20.12.2017 / 09:52
source