EF Core搜索多列null错误

时间:2015-09-22 17:13:20

标签: c# entity-framework asp.net-core entity-framework-core

我正在使用EF Core,我需要能够对多个列进行搜索,但有时一个项目可能为空。

var myObject = _Context.CurrentTransformers.Where(a =>
    a.ID.ToString().Contains(search) ||
    a.ADMSKey.Contains(search) ||
    a.AccuracyClass.ToString().Contains(search) ||
    a.CoreCount.Contains(search) ||
    a.PrimaryCurrentRatio.Contains(search) ||
    a.SecondaryCurrentRatio.Contains(search) ||
    a.EOLXINIVVC.Contains(search));

我知道在以前的版本中我可以使用UseCSharpNullComparisonBehavior但是我无法在EF Core中找到ContextOptions。上面的代码抛出的Object引用没有设置为对象错误的实例。

2 个答案:

答案 0 :(得分:3)

我将PredicateBuilder发布的here扩展为包含名为Search<T>的扩展方法。

你可以这样称呼它;

var myObject = _Context.CurrentTransformers.Search(search);

<强>代码

public static class PredicateBuilder
{
    public static Expression<Func<T, bool>> True<T>() { return f => true; }
    public static Expression<Func<T, bool>> False<T>() { return f => false; }

    public static IQueryable<T> Search<T>(this IQueryable<T> self, string keyword)
    {
        var predicate = False<T>();
        var properties = typeof(T).GetTypeInfo().DeclaredProperties;
        foreach (var propertyInfo in properties)
        {
            if (propertyInfo.GetGetMethod().IsVirtual)
                continue;

            var parameter = Expression.Parameter(typeof(T), "x");
            var property = Expression.Property(parameter, propertyInfo);

            var propertyAsObject = Expression.Convert(property, typeof(object));
            var nullCheck = Expression.NotEqual(propertyAsObject, Expression.Constant(null, typeof(object)));

            var propertyAsString = Expression.Call(property, "ToString", null, null);
            var keywordExpression = Expression.Constant(keyword);
            var contains = Expression.Call(propertyAsString, "Contains", null, keywordExpression);

            var lambda = Expression.Lambda(Expression.AndAlso(nullCheck, contains), parameter);

            predicate = predicate.Or((Expression<Func<T, bool>>)lambda);
        }
        return self.Where(predicate);
    }

    public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2)
    {
        var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
        return Expression.Lambda<Func<T, bool>>(Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters);
    }

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

答案 1 :(得分:0)

我这样做了:

var myObject = _Context.CurrentTransformers.Where(a => 
    a.ID.ToString().Contains(search) ||
    a.ADMSKey != null && a.ADMSKey.ToLower().Contains(search.ToLower()) ||
    a.AccuracyClass != null && a.AccuracyClass.ToString().ToLower().Contains(search.ToLower()) ||
    a.CoreCount != null && a.CoreCount.ToLower().Contains(search.ToLower()) ||
    a.PrimaryCurrentRatio != null && a.PrimaryCurrentRatio.ToLower().Contains(search.ToLower()) ||
    a.SecondaryCurrentRatio != null && a.SecondaryCurrentRatio.ToLower().Contains(search.ToLower()) ||
    a.EOLXINIVVC != null && a.EOLXINIVVC.ToLower().Contains(search.ToLower()));

这样可行,但是那个很脏还是可以做到的?