我遵循这个主题:链接文本
杰森给出了一个例子:
public static Expression<TDelegate> AndAlso<TDelegate>(this Expression<TDelegate> left, Expression<TDelegate> right)
{
return Expression.Lambda<TDelegate>(Expression.AndAlso(left, right), left.Parameters);
}
Run Code Online (Sandbox Code Playgroud)
及其用法如下:
Expression<Func<Client, bool>> clientWhere = c => true;
if (filterByClientFName)
{
clientWhere = clientWhere.AndAlso(c => c.ClientFName == searchForClientFName);
}
if (filterByClientLName)
{
clientWhere = clientWhere.AndAlso(c => c.ClientLName == searchForClientLName);
}
Run Code Online (Sandbox Code Playgroud)
我有一个订单表,我按照上面的例子,更改列名,我得到了帖子创建者有类似的错误
二进制运算符AndAlso没有为类型'System.Func
2[Models.Order,System.Boolean]' and 'System.Func2 [Models.Order,System.Boolean]'定义.
有人对我失踪的事有任何想法吗?
更新:
Eric,我进一步关注了上一篇文章的用户所要求的内容,这里是链接文本
用户有这个
Expression<Func<Client, bool>> clientWhere = c => true;
Expression<Func<Order, bool>> orderWhere = o => true;
Expression<Func<Product, bool>> productWhere = p …Run Code Online (Sandbox Code Playgroud) 我正在尝试在ASP.NET MVC 2应用程序中实现搜索功能.我根据用户输入的条件创建表达式:
public ViewResult FindCustomer( string forename, string familyname, DateTime? dob)
{
Expression<Func<Customer, bool>> searchCriteria = p => (
forename.IsNullOrEmpty() ? true : p.Forename == forename
&& familyname.IsNullOrEmpty() ? true : p.FamilyNames.Any(n => n.Name == familyname)
&& dob.HasValue ? true : p.DOB == dob
);
Run Code Online (Sandbox Code Playgroud)
然后传递给存储库中的方法
IQueryable<Customer> customers = CustomerRepository.FilterBy(searchCriteria);
Run Code Online (Sandbox Code Playgroud)
问题是当我运行它时,我得到以下异常
System.InvalidCastException: Unable to cast object of type 'NHibernate.Hql.Ast.HqlCast' to type 'NHibernate.Hql.Ast.HqlBooleanExpression'
Run Code Online (Sandbox Code Playgroud)
根据这个问题,问题是在表达式中使用条件运算符.
所以我想我必须以其他方式创建Expression,但我不知道该怎么做.我对Linq很新,所以任何帮助都会被感激之情!