Your issue is a basic one, so let's keep Helper, etc., out of it for now.
I try to use the NorthwindIB database when experimenting with and communicating about queries, for the following reasons:
- It's the only database that we (and others who will read this post) have in common.
- It's a database that I actually have, and so can build an actual model against it, and so can do actual queries against, instead of just guessing or trying to reason out what will work.
Once I've worked out a query, I can try to translate it to your database, but will have to guess as the associations between your tables until and if you give me a detailed account (which I'd prefer you didn't unless absolutely necessary).
Associations in NorthwindIB are as follows:
------------------------------------------------------
Customer -----o Order
Employee -----o Order
Order -----o OrderDetail
OrderDetail o----- Product
Product o----- Supplier
A Customer places many Orders: an Employee writes many Orders. An Order has many OrderDetails (line items). An OrderDetail references a single Product, but one Product can of course be represented in many OrderDetails. A Product has a single Supplier, but a Supplier may supply many Products.
Those are the six tables I use for most examples; however, I need to use an additional one in order to have a set of associations similar to what I think yours are:
Employee -----o EmployeeTerritories
Let's consider your query:
IEntityQuery<CustomerLocation> search = EntityManager.CustomerLocations
.Where(cl => cl.Territory.OperationTerritories
.Where(ot => ot.Operation.OperationID == 1));
If I guess the associations from the plurality of the navigation properties, I can state this query in English as follows:
"Show me all CustomerLocations for which any related OperationTerritory's Operation is the one identified by ID=1." I also guess the following associations:
CustomerLocation o----- Territory
Territory -----o OperationTerritories
OperationTerritory o----- Operation
Again, if I've guessed the details of the associations correctly, this is equivalent to the following query against NorthwindIB:
IEntityQuery<EmployeeTerritory> search = EntityManager.EmployeeTerritories
.Where(et => et.Employee.Orders
.Where(o => o.Customer.CustomerID == 1));
"Show me all EmployeeTerritories for which any related Order's Customer is the one identified by ID=1."
I'm going to use a variable, custID to hold the ID value that I need to match:
IEntityQuery<EmployeeTerritory> search = EntityManager.EmployeeTerritories
.Where(et => et.Employee.Orders
.Where(o => o.Customer.CustomerID == custID));
(I need to do that because, in NorthwindIB, Customer.CustomerID is a Guid. But that's just an ancillary detail.)
On the above query I get the following compile time error (last part of query highlighted by Visual Studio):
Cannot implicitly convert type '' to System.Collections.Generic.IEnumerable<DomainModel.Order>' to 'bool'.If I've translated your query correctly, you should be getting that same error (with equivalent types).
So what is the problem?
Well,
LINQ expects a Where expression to evaluate to a boolean; so it doesn't know what to do with the expression
et => et.Employee.Orders.Where(o => o.Customer.CustomerID == custID)
which returns
a collection of Orders.
It literally doesn't know what you're asking it to retrieve.
On the other hand, the following query works:
var empTerritoriesQuery = _mgr.EmployeeTerritories
.Where(et=>et.Employee.Orders.Any(o=>o.Customer.CustomerID == custID));
That's because the expression
et=>et.Employee.Orders.Any(o=>o.Customer.CustomerID == custID)
does evaluate to a boolean.
So I
think that's what you need. Your equivalent would be
IEntityQuery<CustomerLocation> search = _mgr.CustomerLocations
.Where(cl=>cl.Territory.OperationsTerritories.Any(ot=>ot.Operation.OperationID == opID));