2

I have a very simple code of what I am trying to achieve here.

Basically the idea here would be to be able to do a simple FindBy(x => x.<the_column_name>, <the value>); therefore I wouldn't have to copy-paste the same query by changing only the column name.

For now I keep getting an exception from LINQ saying LINQ to Entities does not recognize the method 'System.String Invoke(Spunky.Entities.User)' method, and this method cannot be translated into a store expression

Is that something possible ? Or maybe we're not yet there with EF 6.1?

public class UsersRepository
{
  private Lazy<IDataContext> context;

  public UsersRepository()
    : this(() => new DataContext())
  {
  }

  public UsersRepository(Func<IDataContext> context)
  {
    this.context = new Lazy<IDataContext>(context);
  }

  public IQueryable<User> Find(int id)
  {
    return FindBy(u => u.Id, id);
  }

  public IQueryable<User> Find(string username)
  {
    return FindBy(u => u.Username, username);
  }

  public IQueryable<User> FindBy<T>(Func<User, T> property, T value)
  {
    return context.Value
            .Users
            .Where(u => property.Invoke(u).Equals(value));
  }
}

2 Answers 2

1

If you change the signature

IQueryable<User> FindBy(Expression<Func<User, bool>> predicate)
{
    return context.Users.Where(predicate);
}

you can call

return FindBy(u => u.Username == username);

The code hardly changes, but you don't need to manufacture expressions.

You have to enter an expression (not a Func) because expressions can be translated into SQL. A Func is just a .Net delegate, for which no SQL equivalent exists.

2
  • True it works, but then again, it render the Where useless in the fact that it's exactly the same. :/
    – Erick
    Apr 7, 2014 at 2:44
  • Well, except that you still don't have a Where in the Find methods. It's just a slightly different way of parameterizing the predicate. Apr 8, 2014 at 9:59
0

You need to construct an expression tree linq to entities can translate to sql:

public IQueryable<T> FindBy<TKey>(Expression<Func<T,TKey>> memberExpression, object value)
{
    var parameterVisitor = new ParameterVisitor(memberExpression);
    var parameter = parameterVisitor.Parameter;
    var constant = Expression.Constant(value);
    var equal = Expression.Equal(memberExpression,constant);
    var predicate = Expression.Lambda(equal, parameter);
    return context.Value.Users.Where(predicate);
}

public class ParameterVisitor : ExpressionVisitor
{
   public ParameterExpression Parameter { get;set;}
   public ParameterVisitor(Expression expr)
   {
       this.Visit(expr);
   }

   protected override VisitParameter(ParameterExpression parameter)
   {
        Parameter = parameter;
        return parameter;
    }

}

Still untested from the top of my head.

Edit: Code corrected.

2
  • 1
    Hum, nice idea, but Expression.Equal() render a BinaryExpression and the Where method requires either a Func<T, Bool> or a Expression<Func<T, Bool>> but I'll search with this in mind, maybe there is something to do here.
    – Erick
    Apr 7, 2014 at 2:49
  • You are right. Forgot to make a lambda predicatel. See update. Apr 7, 2014 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.