Tuesday, 15 September 2015

c# - LINQ condition inside where to filter -


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

  IEnumerable & LT; DatatablesViewModel & gt; viewmodel = (in db.tTADTRs a a.EmpID in db.tHREmployees joining b equals c equal join db.tHRJobGrades on b.EmpID a.JobGrdID the db on c.JobGrdID b.ShftID. Joining tTAShifts is equal to d. ShftID b.Dayid on db.tTADays joining e in e.DayID where equals (b.LogDt> = PeriodDates.FromDt & amp; b .LogDt & lt; = PeriodDates.ToDt) new DatatablesViewModel select {EmpID = a.EmpID, name = a.LastNm + "," + a.FirstNm + "," + a.MiddleNm, JobGradeDesc = c.JobGrdDesc, ShftNm = D.ShftNm, DayType = e.DayNm, LogDT = b.LogDt, login = b, logout, logout = b.LogOut, absent = b.AbsDay, task = b.WorkHr, late = b.LateHr, overtime = b.OTHr, Undertime = b.OTHr, Nightdiff = b.NDHr, NightPrem = b.NPHr, Approvedot = b.AppOT, approved OB = Bi.afoobi approved Koa = Bi.apisiooa Approved CS = B.AppCS, Exception = b.ExcptStatus}). Orderbiz (x = & gt; x.EmpID);  

I think I have a dropdown menu in which the employee name is 'ALL' option. I want to add a condition inside the 'where' somthing like code above:

  if (! Parameter.selectedEmp = "all") {where (x => x.Empid = = Ultimate .CustomParam_EmpID) & lt; - Add this filter  

}

I only want to select the query that is necessary to avoid delays in my query.

You do not need to add this to the where section directly. Evaluate IEnumerable and your LINQ query only once you start to redo through the archive. You can just do something like this:

  on relevantVMObjects = (db.tHREmployees in db.tTADTRs on an a.EmpID at b.EmpID one in db.tHRJobGrades b is joining the equivalent of joining e db.tTADays on .JobGrdID c.JobGrdID d.ShftID equal join d db.tTAShifts on b.ShftID b.DayID is e.DayID where (b. LogDt equals & gt; = PeriodDates.FromDt & amp; & B .LogDt & lt; = PeriodDates.ToDt) new DatatablesViewModel select {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, act = b.WorkHr, while = 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.ExcptSta Tus}). Serial (x = & gt; x.EmpID); If (parameter.selectedEmp! = "ALL") relevantVMObjects = relevantVMObjects.Where (x = & gt; x.EmpID == param.CustomParam_EmpID); IEnumerable & LT; DatatablesViewModel & gt; Visual model = relevant web object;  

No comments:

Post a Comment