Can i use reflection in linq for objects?

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 to dynamically generate an expression. When I call the method:

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

      

System error message as below:

"LINQ to Entities does not recognize a method System.Object GetValue(System.Object, System.Object[])

, and this method cannot be translated into the Expression store."

My question is: How can I use reflection in linq for entieies or How can I dynamically reference the property name of an object

thank.

+3


source to share





All Articles