kevin c
kevin c

Reputation: 795

EF Core search multiple columns null error

I am using EF Core and I need to be able to do a search on multiple columns, however sometimes an item maybe null.

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));

I know in previous versions I could use UseCSharpNullComparisonBehavior however I am not able to find ContextOptions inside EF Core. The above code is throwing a Object reference not set to an instance of an object error.

Upvotes: 1

Views: 656

Answers (2)

Stafford Williams
Stafford Williams

Reputation: 9806

I extended the PredicateBuilder posted here to include an extension method called Search<T>.

You can call it like this;

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

Code

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);
    }
}

Upvotes: 3

kevin c
kevin c

Reputation: 795

I did this:

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()));

This works but is that dirty or is that okay to do?

Upvotes: 0

Related Questions