多虧了這樣的回答:How to declare a Linq Expression variable in order to have it processed as a dbParameter
我建議你這樣做:
static Expression<Func<T, bool>> GetExpr<T> (string name, string value)
{
ParameterExpression param = Expression.Parameter(typeof(T), "x");
Expression prop = Expression.Property(param, name); // this is the property name, e.g. .Name
Expression<Func<string>> valueLambda =() => value; // This is the value for == expression.
Expression lookupExpression = Expression.Equal(prop, valueLambda.Body);
Expression<Func<T, bool>> expr = Expression.Lambda<Func<T, bool>>(lookupExpression, param);
return expr;
}
...或者,對。載有():
static Expression<Func<T, bool>> GetExprContains<T>(string name, string[] value)
{
ParameterExpression param = Expression.Parameter(typeof(T), "x");
Expression prop = Expression.Property(param, name); // this is the property name, e.g. .Name
Expression<Func<string[]>> valueLambda =() => value; // This is the value for .Contains() expression.
var mi =
typeof(Enumerable).GetMethods(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)
.FirstOrDefault(x => x.Name == "Contains" && x.GetParameters().Count() == 2)
.MakeGenericMethod(typeof(string)); // Need methodinfo for .Contains(), might want to keep static somewhere
var lookupExpr = Expression.Call(null, mi, valueLambda.Body, prop);
Expression<Func<T, bool>> expr = Expression.Lambda<Func<T, bool>>(lookupExpr, param);
return expr;
}
測試,可與EF 。
要麼使用接口,要麼使用動態變量 – Jbjstam
也不想做。 EF生成這些類,即使可以,也不想在其上添加接口。創建一個動態表達式似乎是最乾淨的方式。 – notlkk
你使用FindAll而不是Where的原因是什麼? – Abion47