5
\$\begingroup\$

Basically I'm looking for a way to improve my code, and avoid making a lot of if statements and parameter combination manually, 'cause I need to return a query result depending in the parameters that were sent.

I've read some posts, and tried some stuffs but nothing that works.

For each if statement and param's combination a report or statistic is generated with the data collected from a data base with EntityFramework according to the params sent and it's displayed in a view to the user.

public class MultipleParameterSearch
{
    public string strCod { get; set; }
    public string strRack { get; set; }
    public string strPosi { get; set; }
    public int? strLvl { get; set; }
    public int? intCantMin { get; set; }
    public int? intCantMax { get; set; }
}

public ActionResult MultipleParameterSearch(MultipleParameterSearch multiple)
{
    if (multiple.strCod != null && multiple.strRack == null && multiple.strPosi == null && multiple.strLvl == null && multiple.intCantMin == null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
               //getData method return a IQueryable<Model>
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Stock > 0).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack != null && multiple.strPosi == null && multiple.strLvl == null && multiple.intCantMin == null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.Partida == multiple.strRack && a.Stock > 0).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack == null && multiple.strPosi != null && multiple.strLvl == null && multiple.intCantMin == null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.Pieza.Contains(multiple.strPosi) && a.Stock > 0).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack == null && multiple.strPosi == null && multiple.strLvl != null && multiple.intCantMin == null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.Pieza.Substring(1).Contains(multiple.strLvl.ToString()) && a.Stock > 0).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack == null && multiple.strPosi == null && multiple.strLvl == null && multiple.intCantMin != null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.Stock >= multiple.intCantMin).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack == null && multiple.strPosi == null && multiple.strLvl == null && multiple.intCantMin == null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db).Where(a => a.Stock <= multiple.intCantMax).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack == null && multiple.strPosi == null && multiple.strLvl == null && multiple.intCantMin != null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var q = getData(db).Where(x => x.Stock >= multiple.intCantMin && x.Stock <= multiple.intCantMax).ToList();
                return PartialView("multi", q);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack == null && multiple.strPosi == null && multiple.strLvl != null && multiple.intCantMin == null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.Stock <= multiple.intCantMax && a.Pieza.Substring(1).Contains(multiple.strLvl.ToString())).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack == null && multiple.strPosi != null && multiple.strLvl == null && multiple.intCantMin == null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.Stock <= multiple.intCantMax && a.Pieza.Contains(multiple.strPosi)).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack != null && multiple.strPosi == null && multiple.strLvl == null && multiple.intCantMin == null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.Stock <= multiple.intCantMax && a.Partida == multiple.strRack).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack == null && multiple.strPosi == null && multiple.strLvl == null && multiple.intCantMin == null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.Stock <= multiple.intCantMax && a.CodProd == multiple.strCod).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack != null && multiple.strPosi == null && multiple.strLvl == null && multiple.intCantMin == null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Partida == multiple.strRack && a.Stock > 0).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack == null && multiple.strPosi != null && multiple.strLvl == null && multiple.intCantMin == null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Pieza.Contains(multiple.strPosi) && a.Stock > 0).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack == null && multiple.strPosi == null && multiple.strLvl != null && multiple.intCantMin == null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Pieza.Substring(1).Contains(multiple.strLvl.ToString()) && a.Stock > 0).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack == null && multiple.strPosi == null && multiple.strLvl == null && multiple.intCantMin != null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Stock >= multiple.intCantMin).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack == null && multiple.strPosi == null && multiple.strLvl != null && multiple.intCantMin != null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.Stock >= multiple.intCantMin && a.Pieza.Substring(1).Contains(multiple.strLvl.ToString())).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack == null && multiple.strPosi != null && multiple.strLvl == null && multiple.intCantMin != null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.Stock >= multiple.intCantMin && a.Pieza.Contains(multiple.strPosi)).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack != null && multiple.strPosi == null && multiple.strLvl == null && multiple.intCantMin != null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.Stock >= multiple.intCantMin && a.Partida == multiple.strRack).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack != null && multiple.strPosi != null && multiple.strLvl == null && multiple.intCantMin == null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.Partida == multiple.strRack && a.Pieza.Contains(multiple.strPosi) && a.Stock > 0).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack != null && multiple.strPosi == null && multiple.strLvl != null && multiple.intCantMin == null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.Partida == multiple.strRack && a.Pieza.Substring(1).Contains(multiple.strLvl.ToString()) && a.Stock > 0).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack == null && multiple.strPosi != null && multiple.strLvl != null && multiple.intCantMin == null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.Pieza == String.Concat(multiple.strPosi,multiple.strLvl) && a.Stock > 0).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack != null && multiple.strPosi != null && multiple.strLvl == null && multiple.intCantMin == null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Partida == multiple.strRack && a.Pieza.Contains(multiple.strPosi) && a.Stock > 0).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack != null && multiple.strPosi == null && multiple.strLvl != null && multiple.intCantMin == null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Partida == multiple.strRack && a.Pieza.Substring(1).Contains(multiple.strLvl.ToString()) && a.Stock > 0).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack != null && multiple.strPosi == null && multiple.strLvl == null && multiple.intCantMin != null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Partida == multiple.strRack && a.Stock >= multiple.intCantMin).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack != null && multiple.strPosi == null && multiple.strLvl == null && multiple.intCantMin == null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Partida == multiple.strRack && a.Stock <= multiple.intCantMax).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack == null && multiple.strPosi != null && multiple.strLvl != null && multiple.intCantMin == null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Pieza == String.Concat(multiple.strPosi, multiple.strLvl) && a.Stock > 0).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack == null && multiple.strPosi != null && multiple.strLvl == null && multiple.intCantMin != null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Pieza.Contains(multiple.strPosi) && a.Stock >= multiple.intCantMin).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack == null && multiple.strPosi != null && multiple.strLvl == null && multiple.intCantMin == null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Pieza.Contains(multiple.strPosi) && a.Stock <= multiple.intCantMax).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack == null && multiple.strPosi == null && multiple.strLvl != null && multiple.intCantMin != null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Pieza.Substring(1).Contains(multiple.strLvl.ToString()) && a.Stock >= multiple.intCantMin).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack == null && multiple.strPosi == null && multiple.strLvl != null && multiple.intCantMin == null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Pieza.Substring(1).Contains(multiple.strLvl.ToString()) && a.Stock <= multiple.intCantMax).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack == null && multiple.strPosi == null && multiple.strLvl == null && multiple.intCantMin != null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Stock >= multiple.intCantMin && a.Stock <= multiple.intCantMax).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack != null && multiple.strPosi != null && multiple.strLvl != null && multiple.intCantMin == null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.Partida == multiple.strRack && a.Pieza == String.Concat(multiple.strPosi, multiple.strLvl) && a.Stock > 0).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack != null && multiple.strPosi != null && multiple.strLvl == null && multiple.intCantMin != null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.Partida == multiple.strRack && a.Pieza.Contains(multiple.strPosi) && a.Stock >= multiple.intCantMin).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack != null && multiple.strPosi != null && multiple.strLvl == null && multiple.intCantMin == null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.Partida == multiple.strRack && a.Pieza.Contains(multiple.strPosi) && a.Stock <= multiple.intCantMax).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack != null && multiple.strPosi == null && multiple.strLvl != null && multiple.intCantMin != null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.Partida == multiple.strRack && a.Pieza.Substring(1).Contains(multiple.strLvl.ToString()) && a.Stock >= multiple.intCantMin).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack != null && multiple.strPosi == null && multiple.strLvl != null && multiple.intCantMin == null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.Partida == multiple.strRack && a.Pieza.Substring(1).Contains(multiple.strLvl.ToString()) && a.Stock <= multiple.intCantMax).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack != null && multiple.strPosi == null && multiple.strLvl == null && multiple.intCantMin != null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.Partida == multiple.strRack && a.Stock >= multiple.intCantMin && a.Stock <= multiple.intCantMax).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack == null && multiple.strPosi != null && multiple.strLvl != null && multiple.intCantMin != null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.Pieza == String.Concat(multiple.strPosi, multiple.strLvl) && a.Stock >= multiple.intCantMin).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack == null && multiple.strPosi != null && multiple.strLvl != null && multiple.intCantMin == null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.Pieza == String.Concat(multiple.strPosi, multiple.strLvl) && a.Stock <= multiple.intCantMax).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack == null && multiple.strPosi == null && multiple.strLvl != null && multiple.intCantMin != null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.Pieza.Substring(1).Contains(multiple.strLvl.ToString()) && a.Stock >= multiple.intCantMin && a.Stock <= multiple.intCantMax).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack != null && multiple.strPosi != null && multiple.strLvl != null && multiple.intCantMin == null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Partida == multiple.strRack && a.Pieza == String.Concat(multiple.strPosi, multiple.strLvl) && a.Stock > 0).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack != null && multiple.strPosi != null && multiple.strLvl == null && multiple.intCantMin != null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Partida == multiple.strRack && a.Pieza.Contains(multiple.strPosi) && a.Stock >= multiple.intCantMin).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack != null && multiple.strPosi != null && multiple.strLvl == null && multiple.intCantMin == null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Partida == multiple.strRack && a.Pieza.Contains(multiple.strPosi) && a.Stock <= multiple.intCantMax).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack != null && multiple.strPosi == null && multiple.strLvl != null && multiple.intCantMin != null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Partida == multiple.strRack && a.Pieza.Substring(1).Contains(multiple.strLvl.ToString()) && a.Stock >= multiple.intCantMin).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack != null && multiple.strPosi == null && multiple.strLvl != null && multiple.intCantMin == null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Partida == multiple.strRack && a.Pieza.Substring(1).Contains(multiple.strLvl.ToString()) && a.Stock <= multiple.intCantMax).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack != null && multiple.strPosi == null && multiple.strLvl == null && multiple.intCantMin != null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Partida == multiple.strRack && a.Stock >= multiple.intCantMin && a.Stock <= multiple.intCantMax).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack == null && multiple.strPosi != null && multiple.strLvl != null && multiple.intCantMin != null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Pieza == String.Concat(multiple.strPosi, multiple.strLvl) && a.Stock >= multiple.intCantMin).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack == null && multiple.strPosi != null && multiple.strLvl != null && multiple.intCantMin == null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Pieza == String.Concat(multiple.strPosi, multiple.strLvl) && a.Stock <= multiple.intCantMax).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack == null && multiple.strPosi != null && multiple.strLvl == null && multiple.intCantMin != null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Pieza.Contains(multiple.strPosi) && a.Stock >= multiple.intCantMin && a.Stock <= multiple.intCantMax).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack != null && multiple.strPosi != null && multiple.strLvl != null && multiple.intCantMin != null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.Partida == multiple.strRack && a.Pieza == String.Concat(multiple.strPosi, multiple.strLvl) && a.Stock >= multiple.intCantMin).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack != null && multiple.strPosi != null && multiple.strLvl != null && multiple.intCantMin == null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.Partida == multiple.strRack && a.Pieza == String.Concat(multiple.strPosi, multiple.strLvl) && a.Stock <= multiple.intCantMax).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack != null && multiple.strPosi != null && multiple.strLvl == null && multiple.intCantMin != null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.Partida == multiple.strRack && a.Pieza.Contains(multiple.strPosi) && a.Stock >= multiple.intCantMin && a.Stock <= multiple.intCantMax).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack == null && multiple.strPosi != null && multiple.strLvl != null && multiple.intCantMin != null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.Pieza == String.Concat(multiple.strPosi, multiple.strLvl) && a.Stock >= multiple.intCantMin && a.Stock <= multiple.intCantMax).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack != null && multiple.strPosi != null && multiple.strLvl != null && multiple.intCantMin != null && multiple.intCantMax == null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Partida == multiple.strRack && a.Pieza == String.Concat(multiple.strPosi, multiple.strLvl) && a.Stock >= multiple.intCantMin).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack != null && multiple.strPosi != null && multiple.strLvl != null && multiple.intCantMin == null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Partida == multiple.strRack && a.Pieza == String.Concat(multiple.strPosi, multiple.strLvl) && a.Stock <= multiple.intCantMax).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack == null && multiple.strPosi != null && multiple.strLvl != null && multiple.intCantMin != null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Pieza == String.Concat(multiple.strPosi, multiple.strLvl) && a.Stock >= multiple.intCantMin && a.Stock <= multiple.intCantMax).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack != null && multiple.strPosi == null && multiple.strLvl != null && multiple.intCantMin != null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Partida == multiple.strRack && a.Pieza.Substring(1).Contains(multiple.strLvl.ToString()) && a.Stock >= multiple.intCantMin && a.Stock <= multiple.intCantMax).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack != null && multiple.strPosi != null && multiple.strLvl == null && multiple.intCantMin != null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Partida == multiple.strRack && a.Pieza.Contains(multiple.strPosi) && a.Stock >= multiple.intCantMin && a.Stock <= multiple.intCantMax).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack != null && multiple.strPosi != null && multiple.strLvl != null && multiple.intCantMin == null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Partida == multiple.strRack && a.Pieza == String.Concat(multiple.strPosi, multiple.strLvl) && a.Stock <= multiple.intCantMax).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod == null && multiple.strRack != null && multiple.strPosi != null && multiple.strLvl != null && multiple.intCantMin != null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.Partida == multiple.strRack && a.Pieza == String.Concat(multiple.strPosi, multiple.strLvl) && a.Stock >= multiple.intCantMin && a.Stock <= multiple.intCantMax).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }
    else if (multiple.strCod != null && multiple.strRack != null && multiple.strPosi != null && multiple.strLvl != null && multiple.intCantMin != null && multiple.intCantMax != null)
    {
        using (ELECTROPEntities db = new ELECTROPEntities())
        {
            try
            {
                var x = getData(db)
                        .Where(a => a.CodProd == multiple.strCod && a.Partida == multiple.strRack && a.Pieza == String.Concat(multiple.strPosi, multiple.strLvl) && a.Stock >= multiple.intCantMin && a.Stock <= multiple.intCantMax).ToList();
                return PartialView("multi", x);
            }
            catch (Exception ex)
            {
                return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
            }
        }
    }

    return Json(multiple, JsonRequestBehavior.AllowGet);
}

I pasted the version with linq instead of query string

\$\endgroup\$
  • \$\begingroup\$ I have rolled back your last edit. Once answers are available, you should be careful to edit a question in order not to invalidate answers. It might be better you either write a self-answer or ask a follow-up question. You could also decide whether to accept any of the answers here. \$\endgroup\$ – dfhwze Aug 9 at 17:22
  • 1
    \$\begingroup\$ @dfhwze Perfect mate, thanks \$\endgroup\$ – Manuel Gonçalves Aug 9 at 17:46
3
\$\begingroup\$

So now basically rewriting the whole answer. As OP asks:

Basically I'm looking for a way to [...] avoid making a lot of if statements and parameter combination manually

This answer provides an alternate solution to tackle exactly that. Precisely, it is easier to maintain and can be adapted to other models as well.

In the whole code I only found a single special case: If strPosi and strLvl are set, the .Where differs. In all other cases you execute the exact same code.

I divided the whole method in 3 methods. Starting with GetParamsToExecute. This method uses GetProperties and GetValue to dynamically generate a list of all parameters not null. Thus, it can be reused for other models as well, but has a costly operation with GetProperties.

private List<string> GetParamsToExecute(object model)
{
    List<string> paramsToExecute = new List<string>();
    foreach (var p in model.GetType().GetProperties())
    {
        if (p.GetValue(model, null) != null)
        {
            paramsToExecute.Add(p.Name);
        }
    }
    return paramsToExecute;
}

Second, I created a method ExecuteParams, this method is model specific and executes all specific .Where clauses for the MultipleParameterSearch model. It covers the special case of strPosi and strLvl in an intial if-statement and covers the rest of the code to execute in a switch case.

private IQueryable<Model> ExecuteParams(IQueryable<Model> source, List<string> paramsToExecute, MultipleParameterSearch multiple)
{
    if (paramsToExecute.Contains("strPosi") && paramsToExecute.Contains("strLvl"))
        {
            source = source.Where(a => a.Pieza == String.Concat(multiple.strPosi, multiple.strLvl));
            paramsToExecute.Remove("strPosi");
            paramsToExecute.Remove("strLvl");
        }

        foreach (var p in paramsToExecute)
        {
            switch (p)
            {
                case "strCod":
                    source = source.Where(a => a.CodProd == multiple.strCod && a.Stock > 0);
                    break;
                case "strRack":
                    source = source.Where(a => a.Partida == multiple.strRack && a.Stock > 0);
                    break;
                case "strPosi":
                    source = source.Where(a => a.Pieza.Contains(multiple.strPosi) && a.Stock > 0);
                    break;
                case "strLvl":
                    source = source.Where(a => a.Pieza.Substring(1).Contains(multiple.strLvl.ToString()) && a.Stock > 0);
                    break;
                case "intCantMin":
                    source = source.Where(a => a.Stock >= multiple.intCantMin);
                    break;
                case "intCantMax":
                    source = source.Where(a => a.Stock <= multiple.intCantMax);
                    break;
                default:
                    throw new ArgumentException(string.Format(
                        CultureInfo.InvariantCulture,
                        "The parameter {0} was not found.",
                        p));
        }
    }

    return source;
}

And lastly the actual MultipleParameterSearch: (EDIT: Added the proper check and return if no parameter is set)

public ActionResult MultipleParameterSearch(MultipleParameterSearch multiple)
{
    var paramsToExecute = GetParamsToExecute(multiple);
    if(!paramsToExecute.Any()) {
        return Json(multiple, JsonRequestBehavior.AllowGet);
    }

    using (ELECTROPEntities db = new ELECTROPEntities())
    {
        try
        {
            var x = ExecuteParams(getData(db), paramsToExecute, multiple).ToList();
            return PartialView("multi", x);
        }
        catch (Exception ex)
        {
            return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
        }
    }
}

It works because whether you execute .Where(ConditionA && ConditionB) or .Where(ConditionA).Where(ConditionB) makes no difference. As my version adds an iterative approach with the the foreach-loop as well as the performance-killer in GetParamsToExecute, it should be less performant than the original. Thus, I'm pretty sure that this is not the optimal solution, but it is easier to maintain then the if cluster before and can be adapted to other models by adding appropriate ExecuteParams methods for these models.

If it is adapted for other models it would be better to have a general ExecuteParams which is called by every action and then chooses the fitting ExecuteParamsForModelX accordingly. This makes your actions uniform and dislocates the execution parts completely.

Action1(ModelType1) {
    ExecuteParams(model);
}

Action2(ModelType2) {
    ExecuteParams(model2);
}

ExecuteParams(object model) {
    if(model.GetType().Equals(typeof(ModelType1)) 
    {
        ExecuteParamsForModelType1(model);
    }
    // or switch-case or whatever ...
}
\$\endgroup\$
3
\$\begingroup\$

SOLVED

this is the way I ended up doing it, don't know if it's the best way, but it works

I took this idea from an answer that was posted here but now it's not anymore, and Honestly I don't remember who suggested it, I took the foreach loop and adapted to my case, for my point of view the foreach loop is the fasted way, and also I can turn it into a method and dive the code to be reusable as suggested by

FeRaac and dfhwze

In the case approached by FeRaac about the exception or the difference between strPosi and strLvl with the rest of the params I changed the way I made the linq so to avoid using LIKE in the where clause.

public ActionResult MultipleParameterSearch(MultipleParameterSearch multiple)
    {
        //First I declare an empty string in which I'll store the different params
        string whereClause = string.Empty;
        //Here I create an empty string list where I'll add not null params values
        List<string> values = new List<string>();
        //Like my data base tables don't have the same names as the params I declare and initialise an string dictionary so I can select the data base table name according the param
        Dictionary<string, string> properties = new Dictionary<string, string>()
        {
            {"strCod", "CodProd" },
            {"strRack", "Rack" },
            {"strPosi", "Position"},
            {"strLvl", "Level" },
            {"intCantMin", "Stock" },
            {"intCantMax", "Stock" }
        };
        //get model properties
        var pro = multiple.GetType().GetProperties();
        //keep count of the iterated params
        int count = 0;
        foreach (var p in pro)
        {
            if (p.GetValue(multiple, null) != null)
            {
                //if the param is not null I create a string in which I add the table name and the param value
                string whereForThisP = string.Format(CultureInfo.InvariantCulture, "{0} " + (p.Name == "intCantMin" ? ">=" : (p.Name == "intCantMax" ? "<=" : "==")) + " @"+count, properties[p.Name]);
                //I add the not null param to the values list
                values.Add(p.GetValue(multiple, null).ToString());
                if (whereClause.Equals(string.Empty))
                {
                   //if whereClause is still ampty just add the string
                    whereClause = whereForThisP;
                }
                else
                {
                    //if not I append a "AND" to the clause
                    whereClause += " and " + whereForThisP;
                }
                count++;
            }
        }
        //if the clause does not contains "Stock" by default I add it greater than 0
        if (!whereClause.Contains("Stock"))
            whereClause += " and Stock > 0";

        try
        {
            //Here just add to the where clause the predicate string , and the values list converted to array
            var x = getData()
                    .Where(whereClause, values.ToArray()).ToList();
            return PartialView("multi", x);
        }
        catch (Exception ex)
        {
            return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
        }
    }

I documented the code the best I could but in brief I just iterate over the model porperties, evaluate if it's not null, and if that's the case I append to a string variable, the table name, and also append the param value to a list where I store the values to later parse it to an array.

After that sent the string where clause and values array as params to the linq where consult.

\$\endgroup\$
  • 1
    \$\begingroup\$ You need to summarise the changes you've made. Otherwise your answer is off-topic. \$\endgroup\$ – t3chb0t Aug 9 at 17:52
  • 1
    \$\begingroup\$ @t3chb0t Also, this answer deserves a full review as well :p \$\endgroup\$ – dfhwze Aug 9 at 17:55
  • \$\begingroup\$ @t3chb0t I added documentation don't know if it's ok this way \$\endgroup\$ – Manuel Gonçalves Aug 9 at 19:50
  • \$\begingroup\$ What's missing is some information about how and/or why you wrote this code. For instance you can write which suggestions you've implemented or not etc. Maybe you've made some other changes on your own or found anything else that would be interesting to know? \$\endgroup\$ – t3chb0t Aug 9 at 19:55
  • \$\begingroup\$ @t3chb0t changed it, and sorry again I'm new here, didn't know that one's must be very specific, and I'm not use to it \$\endgroup\$ – Manuel Gonçalves Aug 10 at 1:21
2
\$\begingroup\$

DRY Principle

You should write DRY (don't repeat yourself) code. Each of your if-blocks contains the following pattern:

using (ELECTROPEntities db = new ELECTROPEntities())
{
    try
    {
        var x = getData(db)
                .Where(predicate).ToList();
        return PartialView("multi", x);
    }
    catch (Exception ex)
    {
        return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
    }
}

where predicate is the only part that is specific to the if-block.

One way to use this pattern is to ask the MultipleParameterSearch parameter to return the predicate for us.

MultipleParameterSearch multiple; // given
var predicate = multiple.GetFilter();

The filter should return a specific predicate given its state.

public Func<ELECTROPEntity, bool> GetFilter()
{
    if (strCod != null && strRack == null 
        && strPosi == null && strLvl == null 
        && intCantMin == null && intCantMax == null)
    {
        return (entity) => entity => entity.CodProd == strCod && entity.Stock > 0;
    }
    // .. and so on
}

MultipleParameterSearch can then be implemented with just a couple of lines:

public ActionResult MultipleParameterSearch(MultipleParameterSearch multiple)
{
     using (var db = new ELECTROPEntities())
     {
         try
         {
             return PartialView("multi", getData(db).Where(multiple.GetFilter()).ToList());
         }
         catch (Exception ex)
         {
             return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
         }
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.