Can I use reflection in linq to entities?

Viewed 72

Below is my code:

public class ExpressionBuilder<T>
{
    public static Expression<Func<T, bool>> BuildStringCondition(string propertyName, string value)
    {
        Expression<Func<T, bool>> tempFilter = p => p.GetType().GetProperty(propertyName).GetValue(p, null).ToString().Contains(value);
        return tempFilter;
    }
}

I just want build an Expression dynamically. When I invoke the method:

Expression<Func<plcontrol, bool>> conditions = ExpressionBuilder<plcontrol>.BuildStringCondition("code", "%001")
int count = _db.plcontrols.Count(conditions);

System reported error like below:

"LINQ to Entities does not recognize the method System.Object GetValue(System.Object, System.Object[]) method, and this method cannot be translated into a store expression."

My question is How can I use reflection in linq to entieies or How can I reference entity property name dynamically

thanks.

0 Answers
Related