I am trying to build up an expression that will be applied to an IQueryable collection.
I can build an expression like this:
[TestClass]
public class ExpressionTests
{
private IQueryable<MyEntity> entities;
private class MyEntity
{
public string MyProperty { get; set; }
}
[TestInitialize]
public void Setup()
{
entities = new[]
{
new MyEntity {MyProperty = "first"},
new MyEntity {MyProperty = "second"}
}.AsQueryable();
}
[TestMethod]
public void TestQueryingUsingSingleExpression()
{
Expression<Func<MyEntity, bool>> expression = e => e.MyProperty.Contains("irs");
Assert.AreEqual(1, entities.Where(expression).Count());
}
}
Now I want to separate the two parts of the expression:
[TestMethod]
public void TestQueryingByCombiningTwoExpressions()
{
Expression<Func<MyEntity, string>> fieldExpression = e => e.MyProperty;
Expression<Func<string, bool>> operatorExpression = e => e.Contains("irs");
// combine the two expressions somehow...
Expression<Func<MyEntity, bool>> combinedExpression = ???;
Assert.AreEqual(1, entities.Where(combinedExpression).Count());
}
Any suggestions as to how I might do this?
Btw the provider that will be resolving the expression is Linq for NHibernate.