1

I have an Employer entity, and the employer has 0 or more Employees. I wish to load the employer (with Id = 16) so that it's Employees navigation property only lists English speakers. So I do this:

_uow.EmployerRepository.FirstOrDefault (
    o => o.Id == 16
    && o.Employees.Any(a => a.Language == "english"),
    "Employees");

Apparently "Any" means, "return all Employee records if any of the employees matches your condition". Using this code my Employer is populated with all of its employees as long as any one of them has their language set to "english".

How do I change the filter it so that the Employees navigation property will only be populated with Employees whose Language property is "english"?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Developer Webs
  • 983
  • 9
  • 29
  • It actually means "Return the first *employer* that has employee with a language of "English". You want all employers; with Employees filtered to English only? – BradleyDotNET Oct 26 '17 at 16:54
  • No, I want one Employer (where id = 16), and only his employees where the employee language is english. Currently the Employer loads correct, but the Employees navigation property is populated with all employees (some that are English and some not) as long as at least one of the employees is set to English. – Developer Webs Oct 26 '17 at 17:52

1 Answers1

2

This looks like a 2 step process, not one.

  1. Get your employer from the database.

  2. Now get all of the English speaking employees from the Employer.Employees.

Another way you could do it is search for just Employees and not the Employer

I am not sure what your structure is exactly, but you could probably do something like this instead:

_uow.EmployeeRepository.Where(x => x.EmployerId = 16 && x.Language == "english")
TyCobb
  • 8,909
  • 1
  • 33
  • 53