Using Where in c # asp.net mvc 5

0

what can I do to save what that where generates every time I enter and then show it in the view, as well as saving the FirstOrDefault that every time I enter it I keep it in that add

        if (DedudccionesId != null)
        {
            IList<string> Deducciones = new List<string>();

                            for (int i = 0; i < DedudccionesId.Length; i++)
            {
                int idDeduccion = DedudccionesId[i];
                var Deduccion = db.Tbl_Deducciones.FirstOrDefault(t => t.DedId == idDeduccion);
                Deducciones.Add(Deduccion.DedDescripcion);
                if (Deduccion.DedTipo == "No Frecuente")
                {
                    var DeduccionesEmpleados = db.Con_DetalleDeduccionesEmpleado.Where(t => t.DedId == idDeduccion && t.AgeId == tbl_Agencia.AgeId && t.AreId == tbl_Area.AreId && DbFunctions.TruncateTime(t.DetDedEmpFecha) >= FechaDesde && DbFunctions.TruncateTime(t.DetDedEmpFecha) <= FechaHasta && t.HisLabEstadoEmpleado == true);
                    ViewBag.DeduccionesEmpleados = DeduccionesEmpleados.ToList();  // muestra las deducciones asignadas a los empleados


                }
                if (Deduccion.DedTipo == "Frecuente")
                {
                    // var DeducionesFijasEmpleado = db.Con_DeduccionesFijasEmpleado.Where(t => t.DedId == tbl_Deducciones.DedId && t.AgeId == tbl_Agencia.AgeId && t.AreId == tbl_Area.AreId && t.HisLabEstadoEmpleado == true);
                    var DeducionesFijasEmpleado = db.Con_DeduccionesFijasEmpleado.Where(t => t.AgeId == tbl_Agencia.AgeId && t.AreId == tbl_Area.AreId && t.HisLabEstadoEmpleado == true);

                    ViewBag.DeducionesFijasEmpleado = DeducionesFijasEmpleado.ToList();   // muestra las deducciones No frecuentes asignadas a los empleados
                }

            }


            ViewBag.Deducciones = Deducciones.ToList();
        }
    
asked by Marco Eufragio 17.10.2018 в 18:00
source

1 answer

2

To use the split and contains you could receive a list separated by commas

string DedudccionesId = "1,2,3";
string[] items = DedudccionesId.Split(',');

var Deducciones = db.Tbl_Deducciones.FirstOrDefault(t => items.Contains(t.DedId));

So you can directly get the list without needing any for or FirstOrDefault() none of that, from the list valid if this content.

Note: I put some values in DedudccionesId as an example

All the code could look something like this

if (Deduccion.DedTipo == "No Frecuente")
{
    string[] items = DedudccionesId == null ? new string[] : DedudccionesId.Split(',');

    var DeduccionesEmpleados = db.Con_DetalleDeduccionesEmpleado.Where(t => items.Contains(t.DedId)
                                                                                && t.AgeId == tbl_Agencia.AgeId && t.AreId == tbl_Area.AreId 
                                                                                && DbFunctions.TruncateTime(t.DetDedEmpFecha) >= FechaDesde 
                                                                                && DbFunctions.TruncateTime(t.DetDedEmpFecha) <= FechaHasta 
                                                                                && t.HisLabEstadoEmpleado == true);
    ViewBag.DeduccionesEmpleados = DeduccionesEmpleados.ToList();  


}
if (Deduccion.DedTipo == "Frecuente")
{
    var DeducionesFijasEmpleado = db.Con_DeduccionesFijasEmpleado.Where(t => t.AgeId == tbl_Agencia.AgeId 
                                                                            && t.AreId == tbl_Area.AreId 
                                                                            && t.HisLabEstadoEmpleado == true);

    ViewBag.DeducionesFijasEmpleado = DeducionesFijasEmpleado.ToList();  
}
    
answered by 17.10.2018 / 23:20
source