-1

我有如下表结构

员工表

ID   Name
1     A
2     B
3     C
4     D
5     E

员工分配表

Alocationid    ID   Date
1              2    26/6/2013
2              2    25/6/2013
3              1    25/6/2013
4              1    24/6/2013
5              3    24/6/2013
6              4    26/6/2013

现在我需要获取特定日期的可用员工。例如,如果我需要 25/6/2013 的可用员工,那么结果将如下所示:-

ID  Name
1   A
3   C
4   D
4

3 回答 3

0

101 Linq是一个很好的参考站点,这个示例似乎最适合您的情况。

于 2013-06-27T09:03:02.740 回答
0

使用组加入来获取每个员工的分配。然后选择那些在特定日期没有任何分配的员工:

from e in db.Employees
join ea in db.EmployeeAllocations 
   on e.ID equals ea.ID into g
where !g.Any(x => x.Date == date)
select e
于 2013-06-27T08:54:17.020 回答
0

如果您正在使用存储库并希望使用较少的类似查询的方法,则可以使用以下方法:

IQueryable<int> allocatedEmployeeIds = AllocationRepository.DbSet.Where(x => x.Date == referenceDate).Select(x => x.ID);
List<Employee> allocatedEmployees = EmployeeRepository.DbSet.Where(x => allocatedEmployeeIds.Any(y => y == x.ID).ToList();

编辑:如果您想要那些未分配的,只需添加“!”

IQueryable<int> allocatedEmployeeIds = AllocationRepository.DbSet.Where(x => x.Date == referenceDate).Select(x => x.ID);
List<Employee> allocatedEmployees = EmployeeRepository.DbSet.Where(x => !allocatedEmployeeIds.Any(y => y == x.ID).ToList();
于 2013-06-27T09:09:00.710 回答