Home > Enterprise >  Getting List() From DB
Getting List() From DB

Time:11-03

public JsonResult SearchApplicantList(string SSession, string ApStatus, string StudyLevel, string SLPrograms)
{
    ab db = new ab();

    var SearchList = db.Students
                       .Where(x => x.SemesterSessioin == SSession  
                                   && x.OverAllStatus == ApStatus 
                                   && x.StudyLvl == StudyLevel 
                                   && x.ProgPref1 == SLPrograms 
                                   && x.ProgPref2 == SLPrograms 
                                   && x.ProgPref3 == SLPrograms 
                                   && x.progPref4 == SLPrograms).ToList();

    return Json(SearchList, JsonRequestBehavior.AllowGet);
}

I want to get a list on the basis of parameters, but few cases the many parameters can be null, null means all.

How can I do this?

CodePudding user response:

Programming. Step by step.

Let me explain:

There is no need to have only one line for the query.

var searchQuery = db.Students (possibly with .Where that is constant).

if (ApStatus != null) {
  searchQuery = searchQuery.Where(x => x.OverAllStatus = ApStatus
}

Btw., plenty of spelling. It is App (not Ap) and Overall not OverAll - it is ONE word as per dictionary.

Anyhow, you can repeat adding where conditions as often as you want, then at the end materialize.

All where conditions are ANDed together.

This is one thing most people overlook - LINQ allows a TON of programming and manipulation of the query tree.

CodePudding user response:

Apply filter by parameter/s which can not be null, for instance SSession and StudyLevel are those parameters which can not be null. So apply filter by them first

var SearchList = db.Students.Where(x => x.SemesterSessioin == SSession && x.StudyLvl == StudyLevel).ToList();

Then check rest of parameters one by one, for SLPrograms will be like:

if(!string.IsNullOrEmpty(SLPrograms))
{
  SearchList = SearchList.Where(Apply Filter By SLPrograms).ToList();
}

And then for rest of Parameters. Remember, use if and if to check each parameters. Like

if(Param1)
{
   //Code
}
if(Param2)
{
   //Code
}

CodePudding user response:

public JsonResult SearchApplicantList(string SSession, string ApStatus, string StudyLevel, string SLPrograms)
{
ab db = new ab();

var SearchList = db.Students
                   .Where(x => (x.SemesterSessioin == SSession || SSession ==null)
                               && (x.OverAllStatus == ApStatus || ApStatus ==null)
                               && (x.StudyLvl == StudyLevel || StudyLevel ==null)
                               && (x.ProgPref1 == SLPrograms || SLPrograms ==null)
                               && (x.ProgPref2 == SLPrograms || SLPrograms ==null)
                               && (x.ProgPref3 == SLPrograms || SLPrograms ==null)
                               && (x.progPref4 == SLPrograms|| SLPrograms == null).ToList();

return Json(SearchList, JsonRequestBehavior.AllowGet);
}
  • Related