I have a self referencing employee table, meaning that there are employees with a primary key "EmployeeID" and a foreign key "SupervisorID" for the parent (supervisor). Managers are on top of the hirarchy, so their SupervisorID is null. There's also a date column (CreatedOn), to filter for specific dates, but only managers have a date, not their employees. I'm trying to get all results from a specific date, managers and their employees, but I just can't get it running. I need to achieve this with LINQ, but I thought I could an SQL query first, to see if I'm somehow able to migrate this to an LINQ query (doesn't matter which syntax). But this was very dellusional, once I realized that it's as hard as in LINQ... I thought that this might be achieved with a CTE, but I can't get the self-reference running at all. Either I just get managers or nothing... Here's a sample table:
------------------------------------------------------
|EmployeeID|Name |SupervisorID|CreatedOn |
|1 |Sanders |NULL |2019-01-01 16:20:33|
|2 |Flanders|1 |NULL |
|3 |Andrews |1 |NULL |
|4 |Ranger |NULL |2019-03-20 18:04:56|
|5 |Hammer |4 |NULL |
|6 |Enderson|4 |NULL |
|7 |Larsson |NULL |2019-10-03 04:55:16|
|8 |Simpson |8 |NULL |
------------------------------------------------------
You can see, that Sanders (2019-01-01 16:20:33) has 2 employees (Flanders, Andrews), Ranger 2019-03-20 18:04:56 has 2 employees (Hammer, Enderson) and Larsson (2019-10-03 04:55:16) has 1 employee (Simpson)
I didn't get anywhere at all with LINQ. If I filter on the date, I don't have any way anymore, to filter on the initial dataset for the employees.
// This filters by date, but then I can't get the employees without date anymore
employees.Where(e => e.CreatedOn >= '2019-01-01' && e.CreatedOn < '2019-01-02')
// This filters all employees with a parent, but then I can't get the managers anymore and can't filter against a date anymore either
employees.Where(e => e.SupervisorID != null)
I tried a join, but that didn't work very well either:
from employees m in employees
join e in employees
on m.EmployeeId equals e.SupervisorID
where m.CreatedOn >= '2019-01-01' && m.CreatedOn < '2019-01-02'
select m
An expected resultset for an appropriate filter would be Sanders and his employees, since Sanders' entry was created on January 1 and since his employees work under him:
------------------------------------------------------
|EmployeeID|Name |SupervisorID|CreatedOn |
|1 |Sanders |NULL |2019-01-01 16:20:33|
|2 |Flanders|1 |NULL |
|3 |Andrews |1 |NULL |
------------------------------------------------------
Does anyone have an idea, how I could achieve this? I won't be able to adjust the database design, since I can only read and another application pushes the data, which I can't change.