使用动态表达式<Func<T,布尔值>>



我有这个代码

public class PropertyFilterSpecification : BaseSpecification<Property>
{
public PropertyFilterSpecification(PropertySearchCriteria sc)
: base(i => 
(!sc.VendorId.HasValue || i.VendorId == sc.VendorId)
&&
(string.IsNullOrWhiteSpace(sc.Name) || i.Name.Contains(sc.Name))
&&
(string.IsNullOrWhiteSpace(sc.Name) || i.Name.Contains(sc.Name))
)
{
AddInclude(b => b.Vendor);
AddInclude($"{nameof(Order.OrderItems)}.{nameof(OrderItem.OrderedProperty)}");
}
}

现在我需要使用动态表达式>

我需要我的代码来添加条件,如果

: base(i => 
addif (!sc.VendorId.HasValue , i.VendorId == sc.VendorId)
.
.addif (string.IsNullOrWhiteSpace(sc.Name) || i.Name.Contains(sc.Name))
.`enter code here`addif 
(string.IsNullOrWhiteSpace(sc.Name) || i.Name.Contains(sc.Name))
)

请问如何建立动态的creitirea。

public static Expression<Func<T, bool>> AndIf<T>(this Expression<Func<T, bool>> expr1, bool condition,
Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
if (condition)
return Expression.Lambda<Func<T, bool>>(Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);
else
return expr1;
}