2

I have a One To many relation (EF) and i want to write a query that filters the One relation AND filters the Many Relation.

For instance: Company has Many Employees

Write a query that filters on Company.Name = "ZonSoft" and where Company.Employees has at least one Employee with the Name "Hesius"

from comp in data.Companies.Include("Employees")
where comp.Name = "Zonsoft" AndAlso comp.Employees.Any(Function(em) em.Name = "Hesius")
select comp

This works fine, but what if the filters are not known at compile time? The user can select out of many filters (name, age, ...) and i don't want to write too much code for that.

i am experimenting with Expression and linqkit but i cannot make the filter work on the Employees relation.

'

exp1 as Expression(Of Func(Of Company, Boolean) = Function(comp) comp.Name = "Zonsoft"
exp2 as Expression(Of Func(Of Employee, Boolean) = Function(emp) emp.Name = "Hesius"

How do i combine these two filters in one query? Or how to i get the desired result?

this works: '

From comp in data.Companies.Include("Employees")
.Where(exp1)
Select comp.

I am looking for something like this:

From comp in data.Companies.Include("Employees")
.Where(exp1 AndAlso comp.Employees.Any(exp2))
Select cc

0 Answers0