I have this method:
public static Expression<Func<MyEntity, bool>> MyMethod(string someId)
{
return o => o.SomeProperty.Equals(someId);
}
This is being passed to another method that uses entity framework with this expression to retrieve the matching items. My problem is it doesn't work. If I replace the someId as part of the Equals call with an actual value it works. However, it doesn't work if I pass the same value into this method above.
Does this mean the someId parameter doesn't get evaluated until the expression is actually used and therefore doesn't contain the actual value? Sorry, getting a bit confused here.
Thanks
Update:
Some debug info for the one that DOES work where the param is hard coded
.Lambda #Lambda1<System.Func`2[MyEntity,System.Boolean]>(MyEntity $t) {\r\n .Call ($t.SomeProperty).Equals(\"test1\")
Some debug info for the one that DOES NOT work where the value is passed into the method
.Lambda #Lambda1<System.Func`2[MyEntity,System.Boolean]>(MyEntity $t) {\r\n .Call ($t.SomeProperty).Equals(.Constant<MyEntity+<>c__DisplayClass4>(MyEntity+<>c__DisplayClass4).someId)
I am guessing when the value is not hard coded the expression has got a reference to something that doesn't exist? Sorry, getting myself in a muddle with what is going on here!
Update 2:
This is where it is being used - a method in a repository class:
public MyEntity Get(string someId)
{
var queryPredicate = MyEntity.MyMethod(someId);
var foundEntity = this.Query(queryPredicate);
}
Query is a method on the repository base class as below:
public IEnumerable<TEntity> Query(Expression<Func<TEntity, bool>> predicate)
{
if (predicate == null)
{
throw new ArgumentNullException("predicate");
}
using (var context = new TContext())
{
return context.CreateObjectSet<TEntity>().AsExpandable().Where(predicate).ToList();
}
}