c# - 从 lambda 表达式中排除 null 或空字符串值

标签 c# lambda expression-trees specification-pattern

在我的应用程序的 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 或空值,它也不起作用。

我该如何解决这个问题并排除我有 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) 返回 true 那么 customerFiler.Name.Contains(Customer.Name); 将不会评估。

关于c# - 从 lambda 表达式中排除 null 或空字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50210550/

相关文章:

c# - 验证多个属性需要一个

c++ - 具有在编译时确定的参数数量的 Lambda 函数

linq - 如何在我的lambda表达式中使用带有2个参数的方法

c# - C#获取单个文件最近创建时间的路径

c# - 如何为动态生成标题文本的数据网格标题添加工具提示?

Java 的 Lambda 流过滤器计数比 For 和 Foreach 循环慢

c# - 在多个过滤器中使用相同的 lambda 参数

c# - 是否可以(以编程方式)证明两个 LINQ 查询相等?

c# - 如何在异常情况下正确使用带有 WCF 服务的 IParameterInspector 来记录 Web 请求?