I have many entities which were generated by EF against the existing database. Most of these entities have a "name" property. I would like a method which takes in an expression against a generic type, and passes in a string value which will ultimately be compared against the name property of the underlying entity.
So far, I have something like this:
T SpecialLookup(DbSet<T> dbSet, Func<T, string, bool> exp, string specialParam)
where T: class, new() {
//here we can do something with specialParam first like clean it up.
//now pass in the specialParam to the underlying query which can
//ultimately look up a matching object by "name == specialParam".
var predicate = PredicateBuilder.New<T>(x => exp(x, specialParam));
var obj = dbSet.AsExpandable().SingleOrDefault(predicate);
return obj;
}
the idea here is then I can call the lookup function and pass in a special value, and a delegate which will be called upon the DbSet using the special value as a param.
SpecialLookup(dbContext.SomeEntities,
delegate(SomeEntity obj, string name) {
return obj.name == name;
},
" special"
);
I am using LinkqKit here to try to make this work but I get the error "unable to cast FieldExpression to LambdaExpression."
My first question is: is there a better/simpler way to make this happen? Am I complicating this too much? Do I even need to use LinqKit?
Second: how can I get past this error?