d3bug
d3bug

Reputation: 149

LINQ condition inside where to filter

Im using ASP MVC, I have this LINQ to get employees DTR:

 IEnumerable<DatatablesViewModel> viewmodel = 
     (from a in db.tHREmployees
      join b in db.tTADTRs on a.EmpID equals b.EmpID
      join c in db.tHRJobGrades on a.JobGrdID equals c.JobGrdID
      join d in db.tTAShifts on b.ShftID equals d.ShftID
      join e in db.tTADays on b.DayID equals e.DayID
      where (b.LogDt >= PeriodDates.FromDt && b.LogDt <= PeriodDates.ToDt)
      select new DatatablesViewModel
      {
          EmpID = a.EmpID,
          Name = a.LastNm + ", " + a.FirstNm + ", " + a.MiddleNm,
          JobGradeDesc = c.JobGrdDesc,
          ShftNm = d.ShftNm,
          DayType = e.DayNm,
          LogDT = b.LogDt,
          LogIn = b.LogIn,
          LogOut = b.LogOut,
          Absent = b.AbsDay,
          Work = b.WorkHr,
          Late = b.LateHr,
          Overtime = b.OTHr,
          Undertime = b.OTHr,
          Nightdiff = b.NDHr,
          NightPrem = b.NPHr,
          ApprovedOT = b.AppOT,
          ApprovedOB = b.AppOB,
          ApprovedCoa = b.AppCOA,
          ApprovedCs = b.AppCS,
          Exception = b.ExcptStatus
      }).OrderBy(x => x.EmpID);

In my view i have dropdown menu that has employee names with 'ALL' option. I want to add a condition inside 'where' in above code somthing like:

if(parameter.selectedEmp != "ALL"){
Where(x => x.EmpID == param.CustomParam_EmpID) <-- add this filter

}

I want to select only what is needed in my query to avoid delay.

Upvotes: 0

Views: 286

Answers (2)

Manish Parakhiya
Manish Parakhiya

Reputation: 3798

You can do this by OR condition

string selectedEmp=parameter.selectedEmp;
IEnumerable<DatatablesViewModel> viewmodel = 
     (from a in db.tHREmployees
      join b in db.tTADTRs on a.EmpID equals b.EmpID
      join c in db.tHRJobGrades on a.JobGrdID equals c.JobGrdID
      join d in db.tTAShifts on b.ShftID equals d.ShftID
      join e in db.tTADays on b.DayID equals e.DayID
      where (b.LogDt >= PeriodDates.FromDt && b.LogDt <= PeriodDates.ToDt) &&
      (selectedEmp != "ALL" || a.EmpID == param.CustomParam_EmpID)
 select new DatatablesViewModel
      {
           ...
      }

Upvotes: 0

toadflakz
toadflakz

Reputation: 7944

You don't need to add it in the Where clause directly. The IEnumerable and your LINQ query are only evaluated once you start to iterate through the collection. You could simply do something like this:

var relevantVMObjects = 
 (from a in db.tHREmployees
  join b in db.tTADTRs on a.EmpID equals b.EmpID
  join c in db.tHRJobGrades on a.JobGrdID equals c.JobGrdID
  join d in db.tTAShifts on b.ShftID equals d.ShftID
  join e in db.tTADays on b.DayID equals e.DayID
  where (b.LogDt >= PeriodDates.FromDt && b.LogDt <= PeriodDates.ToDt)
  select new DatatablesViewModel
  {
      EmpID = a.EmpID,
      Name = a.LastNm + ", " + a.FirstNm + ", " + a.MiddleNm,
      JobGradeDesc = c.JobGrdDesc,
      ShftNm = d.ShftNm,
      DayType = e.DayNm,
      LogDT = b.LogDt,
      LogIn = b.LogIn,
      LogOut = b.LogOut,
      Absent = b.AbsDay,
      Work = b.WorkHr,
      Late = b.LateHr,
      Overtime = b.OTHr,
      Undertime = b.OTHr,
      Nightdiff = b.NDHr,
      NightPrem = b.NPHr,
      ApprovedOT = b.AppOT,
      ApprovedOB = b.AppOB,
      ApprovedCoa = b.AppCOA,
      ApprovedCs = b.AppCS,
      Exception = b.ExcptStatus
  }).OrderBy(x => x.EmpID);

 if(parameter.selectedEmp != "ALL")
      relevantVMObjects = relevantVMObjects.Where(x => x.EmpID == param.CustomParam_EmpID);
 IEnumerable<DatatablesViewModel> viewmodel = relevantVMObjects;

Upvotes: 1

Related Questions