我想使用动态表达式在我的应用中创建高级搜索。我读了一些关于表达式的文章并编写了这段代码:
var result = //my query
from filter in context.Filters
join customer in context.Customers on filter.customerId equals customer.id
select new { customer, filter };
var paramCustomer = Expression.Parameter(typeof(Models.Customer), "customer");
var paramFilter = Expression.Parameter(typeof(Models.Filter), "filter");
var pName = Expression.Property(paramCustomer, "name");
var pInstallDate = Expression.Property(paramFilter, "installDate");
var con1 = Expression.OrElse(
Expression.Equal(pName, Expression.Constant("ASD"))
,
Expression.Equal(pInstallDate, Expression.Constant(13920505))
);
var con2 = Expression.AndAlso(
Expression.Equal(pName, Expression.Constant("ABC"))
,
con1
);
var exp1 = Expression.Lambda<Func<Models.Customer, Models.Filter, bool>>(con2, new ParameterExpression[] { paramCustomer, paramFilter });
Console.WriteLine(
exp1 //output: (customer, filter) => ((customer.name == "ABC") AndAlso ((customer.name == "ASD") OrElse (filter.installDate == 1392)))
);
Console.WriteLine(
result.Where(exp1) //error
);
我想在我的查询中添加动态表达式,但是我收到了这些错误:
错误7'System.Linq.IQueryable'不包含'Where'的定义和最佳扩展方法重载'System.Linq.Enumerable.Where(System.Collections.Generic.IEnumerable,System.Func)'具有一些无效的论点
错误8参数2:无法从'System.Linq.Expressions.Expression&gt;'转换到'System.Func'