简体   繁体   English

LINQ条件在哪里过滤

[英]LINQ condition inside where to filter

Im using ASP MVC, I have this LINQ to get employees DTR: 我正在使用ASP MVC,我使用此LINQ来使员工获得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: 我想在上面的代码中的“ where”里面添加一个条件,例如:

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. 我只想选择查询中需要的内容,以避免延迟。

You don't need to add it in the Where clause directly. 您不需要直接在Where子句中添加它。 The IEnumerable and your LINQ query are only evaluated once you start to iterate through the collection. 仅当您开始遍历集合时,才会对IEnumerable和LINQ查询进行评估。 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;

You can do this by OR condition 您可以按OR条件执行此操作

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
      {
           ...
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM