简体   繁体   English

C#Linq谓词生成从列表中删除所有空值

[英]C# Linq predicate generation remove all null values from list

I am trying to generate filter code using linq predicates. 我正在尝试使用linq谓词生成过滤器代码。 When I use a predicate with a object != null: 当我对对象使用谓词!= null时:

public static Expression<Func<ContactPermitsSearch, bool>> PermitNumberNotNull()
    {
        Expression<Func<ContactPermitsSearch, bool>> predicate = contactPermit => contactPermit.PermitNumber != null;
        return predicate;
    }

which is called by: 称为:

public static IQueryable<ContactPermitsSearch> FilterByNameMailingPermit(this IQueryable<ContactPermitsSearch> queryable, string search, bool filterOn)
    {
        var predicate = PredicateBuilder.True<ContactPermitsSearch>();

        predicate = predicate.And(PermitNumberNotNull());

        var filtered = queryable.AsExpandable().Where(predicate);
        return filtered;
    }

The SQL statement that is generated does not include the PermitNumberNotNull predicate statement. 生成的SQL语句不包含PermitNumberNotNull谓词语句。

Thoughts on fixing this? 关于修复此问题的想法?

There's nothing obviously wrong with the code. 代码显然没有错。 I'm sure that if you inspect predicate in the debugger after merging it with PermitNumberNotNull() you'll see that the not null predicate is part of the expression tree. 我敢肯定,如果在与PermitNumberNotNull()合并后在调试器中检查predicate ,您会看到非null谓词是表达式树的一部分。

The only reasonable assumption is that EF knows that contactPermit.PermitNumber can't ever be null because it's required property. 唯一合理的假设是,EF知道contactPermit.PermitNumber永远不能为null,因为它是必填属性。 Therefore, it simply ignores the predicate. 因此,它只是忽略了谓词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM