I am building the SQL WHERE clause dynamically using the System.Linq.Expressions.Expression class. It works well for simple articles, for example. to add a PhaseCode = X clause, I do the following:
var equalTarget = Expression.Constant(phaseCode, typeof(int?)); var phaseEquals = Expression.Equal(Expression.PropertyOrField(projParam, "PhaseCode"), equalTarget);
However, now I am trying to create an expression that will return a record if the project has been assigned to a specific group. The project and the group have many-to-many relationships. Without expression trees, I would do it like this:
db.Projects.Where(p => .... && p.GroupsAssigned.Any(g => g.ID == groupId))
However, I cannot find a way to express this with the Expression class. There are actually two things that I cannot understand:
- How to navigate between tables
- How to make x.Any ()
Any help is greatly appreciated.
Alec bryte
source share