I want to 'build' a combined query for Subsonic 3.0.0.3, what is the best way for this?
I tried;
Expression<Func<Person, bool>> exp = p => true;
Expression<Func<Person, bool>> fContinent = p => p.ContinentID == 1;
Expression<Func<Person, bool>> fType = p => p.TypeID == 1;
exp = Expression.Lambda<Func<Person, bool>>(Expression.AndAlso(exp, fContinent), exp.Parameters);
exp = Expression.Lambda<Func<Person, bool>>(Expression.AndAlso(exp, fType), exp.Parameters);
var personList = Person.Find(exp);
But that will give the exception "The binary operator AndAlso is not defined …"
I also tried using predicates but that will throw exceptions as well (Expression.Invoke is not supported).
In subsonic 2 I would have used the SqlQuery object, but I would like to know the proper way to do this in version 3 using linq / expressions.
Best Answer
Have you tried
And
instead ofAndAlso
?The right way to do this is to combine the lambda expression bodies, like this:
Even if
And
is supported by your query provider, you'll also need to replace the parameter references infContinent
's Body with references to the parameter defined inexp
--as is, your two expression bodies (combined withAnd
) reference two distinct parameters, each namedp
.See my answer to this question for the cleanest method to replace expression parameters.