从 lambda 表达式中排除 null 或空字符串值
Exclude null or empty string value from lambda expression
在我的应用程序 GetAll
函数中,我有一个名为 (CustomerModel
) 的参数。我用它对查询进行一些过滤,并使用规范模式来避免使用 if-else
:
public async Task<List<CustomerModel>> GetAllAsync(CustomerModel customer, Order order = Order.Ascending, int pageIndex = 1, int pageSize = int.MaxValue)
{
var skip = (pageIndex - 1) * pageSize;
var filter = new CustomerNameSpecification(customer)
.And(new CustomerNoSpecification(customer))
.And(new CustomerCompanySpecification(customer))
.And(new CustomerPhoneSpecification(customer))
.And(new CustomerEmailSpecification(customer))
.And(new CustomerAddressSpecification(customer))
.Take(pageSize)
.Skip(skip);
var orderSpecification = new CustomerOrderSpecification(order);
return await _customerRepository.GetAllAsync(filter, orderSpecification);
}
例如规范对象之一 (CustomerNameSpecification
) :
public class CustomerNameSpecification : Specification<Customer>
{
public CustomerModel Customer { get; set; }
public CustomerNameSpecification(CustomerModel customerModel)
{
Customer = customerModel;
}
public override Expression<Func<Customer, bool>> AsExpression()
{
return customerFiler =>
customerFiler.Name.Contains(Customer.Name);
}
}
UPDATE
并在规范模式中运行:
public class AndSpecification<T> : Specification<T>
where T : class
{
private readonly ISpecification<T> _left;
private readonly ISpecification<T> _right;
public AndSpecification(ISpecification<T> left, ISpecification<T> right)
{
_left = left;
_right = right;
}
public override Expression<Func<T, bool>> AsExpression()
{
var leftExpression = _left.AsExpression();
var rightExpression = _right.AsExpression();
var parameter = leftExpression.Parameters.Single();
var body = Expression.AndAlso(leftExpression.Body, SpecificationParameterRebinder.ReplaceParameter(rightExpression.Body, parameter));
return Expression.Lambda<Func<T, bool>>(body, parameter);
}
}
}
这些链在末尾生成一个 lambda 表达式,存储库使用它来过滤查询。
当 CustomerModel
的每个字段都有一个值时,此解决方案工作正常,但即使 属性 有一个空值或空值,它也不起作用。
如何解决此问题并排除我有 null 或空字符串值的 lambda 表达式?
How can I fix this problem and exclude lambda expression where I have
a null or empty string value?
例如CustomerNameSpecification
,要排除空值你可以使用代码:
public override Expression<Func<Customer, bool>> AsExpression()
{
return customerFiler => string.IsNullOrWhiteSpace(customerFiler.Name) ||
customerFiler.Name.Contains(Customer.Name);
}
如果string.IsNullOrWhitespace(customerFiler.Name)
returns true
那么customerFiler.Name.Contains(Customer.Name);
将不会被评估。
在我的应用程序 GetAll
函数中,我有一个名为 (CustomerModel
) 的参数。我用它对查询进行一些过滤,并使用规范模式来避免使用 if-else
:
public async Task<List<CustomerModel>> GetAllAsync(CustomerModel customer, Order order = Order.Ascending, int pageIndex = 1, int pageSize = int.MaxValue)
{
var skip = (pageIndex - 1) * pageSize;
var filter = new CustomerNameSpecification(customer)
.And(new CustomerNoSpecification(customer))
.And(new CustomerCompanySpecification(customer))
.And(new CustomerPhoneSpecification(customer))
.And(new CustomerEmailSpecification(customer))
.And(new CustomerAddressSpecification(customer))
.Take(pageSize)
.Skip(skip);
var orderSpecification = new CustomerOrderSpecification(order);
return await _customerRepository.GetAllAsync(filter, orderSpecification);
}
例如规范对象之一 (CustomerNameSpecification
) :
public class CustomerNameSpecification : Specification<Customer>
{
public CustomerModel Customer { get; set; }
public CustomerNameSpecification(CustomerModel customerModel)
{
Customer = customerModel;
}
public override Expression<Func<Customer, bool>> AsExpression()
{
return customerFiler =>
customerFiler.Name.Contains(Customer.Name);
}
}
UPDATE
并在规范模式中运行:
public class AndSpecification<T> : Specification<T>
where T : class
{
private readonly ISpecification<T> _left;
private readonly ISpecification<T> _right;
public AndSpecification(ISpecification<T> left, ISpecification<T> right)
{
_left = left;
_right = right;
}
public override Expression<Func<T, bool>> AsExpression()
{
var leftExpression = _left.AsExpression();
var rightExpression = _right.AsExpression();
var parameter = leftExpression.Parameters.Single();
var body = Expression.AndAlso(leftExpression.Body, SpecificationParameterRebinder.ReplaceParameter(rightExpression.Body, parameter));
return Expression.Lambda<Func<T, bool>>(body, parameter);
}
}
}
这些链在末尾生成一个 lambda 表达式,存储库使用它来过滤查询。
当 CustomerModel
的每个字段都有一个值时,此解决方案工作正常,但即使 属性 有一个空值或空值,它也不起作用。
如何解决此问题并排除我有 null 或空字符串值的 lambda 表达式?
How can I fix this problem and exclude lambda expression where I have a null or empty string value?
例如CustomerNameSpecification
,要排除空值你可以使用代码:
public override Expression<Func<Customer, bool>> AsExpression()
{
return customerFiler => string.IsNullOrWhiteSpace(customerFiler.Name) ||
customerFiler.Name.Contains(Customer.Name);
}
如果string.IsNullOrWhitespace(customerFiler.Name)
returns true
那么customerFiler.Name.Contains(Customer.Name);
将不会被评估。