Home » c# » c# – Improving Linq Many-to-Criteria-to-Many Comparisons-Exceptionshub

c# – Improving Linq Many-to-Criteria-to-Many Comparisons-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

There are hundreds of many-to-many questions here on SO, but I don’t see anything that quite relates to my question. Please let me know if I missed something.

I’m trying to find the best way to write code to bridge the relationships between my models. On the one side, Employees and their EmployeeSchedules have a many-to-many relationship, and on the other side, Operations and their JobSchedules have a many-to-many relationship. In between EmployeeSchedules and JobSchedules, however, there is not a direct relationship at all. Instead, there is criteria matching. A JobSchedule matches with a set of EmployeeSchedules if its Crew and Date properties match the EmployeeSchedules‘s Crew and Date values.

I put some sample code on github here, so you can see the models, queries, and code calls I’m making.

Imaginary Question:
Who is scheduled to work on Bob’s crew on Concrete Operation 2?

Imaginary rows in the data tables:

Employees

  1. Bob
  2. Jim
  3. Larry

Crews

  1. Concrete Crew
  2. Mill Crew

Employee Schedules

  1. Bob 1/1/2020 Mill Crew
  2. Bob 1/2/2020 Concrete Crew
  3. Larry 1/2/2020 Mill Crew
  4. Jim 1/2/2020 Concrete Crew

Operation Schedules

  1. Concrete Crew, 1/1/2020, Concrete Operation 1
  2. Mill Crew, 1/1/2020, Mill Operation 1
  3. Mill Crew, 1/2/2020, Mill Operation 1
  4. Concrete Crew 1/2/2020 Concrete Operation 2
  5. Concrete Crew 1/3/2020 Concrete Operation 2

Operations

  1. Concrete Operation 1
  2. Mill Operation 1
  3. Concrete Operation 2

Desired Result:
Crew Members for ‘Concrete Operation 2’: Bob and Jim

Painful though it may be, here’s the code that I currently am using to accomplish this.

//Get Employee's employee schedules
var allEmpSched = db.EmployeeSchedules;
var empSchedules = allEmpSched.Where(x => x.Date >= DateTime.Today);
var specificEmpSched = empSchedules.Where(x => x.Employee == employee);

//Get date list
var empDates = specificEmpSched.Select(x => x.Date);

//Get crew list
var empCrews = specificEmpSched.Select(x => x.Crew);

//Get Operation's job schedules
var AllSchedules = db.JobSchedules;
var jobSchedules = AllSchedules.Where(x => x.Date >= DateTime.Today && x.Operation == operation);

var matchJobSchedules = new List<JobSchedule>();

//Narrow job Schedules to employee matches
foreach (var empSched in specificEmpSched)
{
    var matches = jobSchedules.Where(x => x.Date == empSched.Date && x.Crew == empSched.Crew);
    matchJobSchedules.AddRange(matches);
}

//Desired results are any crew members (employee schedules) for the specific set of dates and crews
var result = new List<EmployeeSchedule>();

foreach (var jobSched in matchJobSchedules)
{
    var matches = empSchedules.Where(x => x.Date == jobSched.Date && x.Crew == jobSched.Crew);
    result.AddRange(matches);
}

return result;

I’m pretty new to all of this, so thanks in advance for any and all help!

How to&Answers: