我有这个方法
public Expression<Func<Auction, bool>> GetUnsetDatesAuctionsExpression()
{
if (condition)
return GetAllUnsetDatesAuctionsExpression();
else
return (a =>
(membershipUser.ProviderUserKey != null) &&
(a.OwnerReference == (Guid)membershipUser.ProviderUserKey) &&
((a.Starts == null) || (a.Ends == null)));
}
}
调用这个方法
private Expression<Func<Auction, bool>> GetAllUnsetDatesAuctionsExpression()
{
return (a => (a.Starts == null) || (a.Ends == null));
}
问题是在上面的公共(public)方法中我有下面一行
(a.Starts == null) || (a.Ends == null)
与私有(private)方法中的表达式主体相同。
现在,这样做当然行不通,因为您不能对 bool 值和表达式进行 AND
return (a =>
(membershipUser.ProviderUserKey != null) &&
(a.OwnerReference == (Guid)membershipUser.ProviderUserKey) &&
(GetAllUnsetDatesAuctionsExpression));
所以,问题是,如何将对私有(private)方法的调用与
结合起来(membershipUser.ProviderUserKey != null) &&
(a.OwnerReference == (Guid)membershipUser.ProviderUserKey)
请您参考如下方法:
创建一个新表达式作为两者的组合,参见 BinaryExpression :
System.Linq.Expressions.BinaryExpression binaryExpression =
System.Linq.Expressions.Expression.MakeBinary(
System.Linq.Expressions.ExpressionType.AndAlso,
firstExpression,
secondExpression);
