SELECT语句中的LINQ附加过滤器

时间:2019-01-31 02:14:53

标签: sql-server linq

我不熟悉LINQ,并且试图修改现有查询。

下面是我要添加的代码段和过滤器:

ViewData["Employees"] = emps = (from staff in db.Staffs
                                from jobinfo in db.JobInfo
                                .Where(x => x.staff_id == staff.StaffID)
                                .OrderByDescending(x => x.jobinfo_id).Take(1)
                                select new { staff, jobinfo })

.Select(x => x.staff).Distinct().OrderBy(x => x.Alias).ToList();

*** Insert Additional filter at the .Select statement above 
<where (jobinfo.last_date == null)>

我可以知道怎么做吗?

2 个答案:

答案 0 :(得分:1)

您能不能像下面那样尝试并检查第三行?

.Where(x => x.staff_id == staff.StaffID && x.last_date == null) 

答案 1 :(得分:0)

两个选择之一是使用&&,然后使用另一个,如下所示

   ViewData["Employees"] = emps = (from staff in db.Staffs
                                from jobinfo in db.JobInfo
                                .Where((x => x.staff_id == staff.StaffID) && (jobinfo.last_date == null))
                                .OrderByDescending(x => x.jobinfo_id).Take(1)
                                select new { staff, jobinfo })

.Select(x => x.staff).Distinct().OrderBy(x => x.Alias).ToList();

其他选项是

 ViewData["Employees"] = emps = (from staff in db.Staffs
                                from jobinfo in db.JobInfo
                                .Where((x => x.staff_id == staff.StaffID)).Where(jobinfo.last_date == null)
                                .OrderByDescending(x => x.jobinfo_id).Take(1)
                                select new { staff, jobinfo })

.Select(x => x.staff).Distinct().OrderBy(x => x.Alias).ToList();