Yeah expected
See something like the following
employees.ToList().Count;
or
employees.ToEntityCollection2().Count();
Fetches the entities first, then it counts the entities in the returned collection.
But the following:
employees.Count();
Executes the count on the database side.
And if you inspect the generated query you will find it produces a lot of duplicates on the database side. Thus the 2393 count, but in fact there are only 7 distinct entities out there.
If there was no Image field in the Employee entity, I'd have suggested trying the following:
employees.Distinct().Count();
Which would have produced DISTINCT in the query.
You can verify what I'm saying by executing the following code:
var q = from e in metaData.Employee
from order in e.Orders
from et in e.EmployeeTerritories
where e.Orders.Any(o => o.ShipCity == "Reims") || e.Orders.Any(o => o.ShipCity == "Lyon")
select e.EmployeeId;
var count = q.ToList().Count; // count = 2393
count = q.Distinct().Count(); // count = 7
The first count returns the entire result set. (duplicates) as we are only fetching the EmployeeId and nothing dictates a distinct resultSet. (While an EntityCollection in your example forced distinct entities to be added).
The second count emits DISTINCT in the generated SQL.