嘗試使用表達式樹動態生成lambda表達式對您的數據。
下面是一個示例實現:
void Main()
{
var fieldName = "LastName";
var value = "test";
var db = new List<Person>() {
new Person() { name = "fred jones", sex = "male", age = 55 },
new Person() { name = "samantha jones", sex = "female", age = 45 },
new Person() { name = "cindy jones", sex = "female", age = 6 }
};
// single query
db.Where(Person.GetFilter("sex", "==", "female").Compile()).Dump();
// OR example
db.Where(
PredicateBuilder.Or<Person>(
Person.GetFilter("sex", "==", "male"),
Person.GetFilter("age", "<", 50)
).Compile()
).Dump();
}
class Person
{
public string name;
public string sex;
public int age;
public static Expression<Func<Person,bool>> GetFilter<T>(string column, string @operator, T value)
{
var ops = new Dictionary<string, Func<Expression, Expression, Expression>>() {
{ "==", (x,y) => Expression.Equal(x,y) },
{ "<=", (x,y) => Expression.LessThanOrEqual(x,y) },
{ ">=", (x,y) => Expression.GreaterThanOrEqual(x,y) },
{ ">", (x,y) => Expression.GreaterThan(x,y) },
{ "<", (x,y) => Expression.LessThan(x,y) },
};
var param = Expression.Parameter(typeof(Person));
var deref = Expression.PropertyOrField(param, column);
var testval = Expression.Constant(value);
return Expression.Lambda<Func<Person,bool>>(
ops[@operator](deref, testval),
param);
}
}
注意我用Linqpad測試這使更換使用.dump()有什麼在你的顯示數據的情況下相應的呼叫。它要求PredicateBuilder類構建OR或AND語句。如果錯誤類型作爲值參數傳遞(比較年齡時傳遞的字符串,例如,拋出「InvalidOperationException:未爲類型'System.String'和'System.Int32'定義二元運算符Equal' 「)。
如果有人能告訴我一個更好的方法來處理將一個運算符的字符串表示轉換爲Expression類,讓我知道,因爲我對上述方式並不滿意,但這是我能想到的最快在當時。