score:2

Accepted answer

Couldn't you just do something like this?

[HttpPost]
public List<TestRecord> Search(TestRecord testRecord){

    List<TestRecord> records = _db.TestRecords
                 .Where(tr => String.IsNullOrEmpty(testRecord.TestType) ? true : tr.TestType == testRecord.TestType)
                 .Where(tr => String.IsNullOrEmpty(testRecord.Part.Name) ? true : tr.Part.Name == testRecord.Part.Name)
                 //etc...
                 .ToList();

    return records;
}

Essentially only filter if the input is there for every field in 1 big query?

score:0

The query doesnt have to be one single chain, you can split it up and insert some if:

var query = _db.TestRecords.AsQueryable();
if (string.IsNullOrEmpty(testRecord.TestType))
{
    query = query.Where(x => x.TestType == testRecord.TestType);
}
if (string.IsNullOrEmpty(testRecord.Part.Name))
{
    query = query.Where(x => x.Part.Name == testRecord.Part.Name);
}

// or, you can use an intermediate variable before returning to debug
return query.ToList();

score:0

I usually use an extension method like this:

public static IQueryable<T> Where<T>(this IQueryable<T> that, object notNull, Expression<Func<T, bool>> predicate)
{
    if (!string.IsNullOrWhiteSpace(notNull?.ToString()))
    {
        return that.Where(predicate);
    }

    return that;
}

Then you can compose your LINQ query like this:

return s.Query()
    .Where(onlyStatus, p => p.Status == onlyStatus)
    .OrderByDescending(p => p.CreatedDate)
    .ToList();

score:0

If you have only one class that has this requirement with only a limited number of properties, say less than 20, I wouldn't bother creating a generic solution for this. Code a Where that checks all properties. This has the advantage that if in future someone changes or removes a property your compiler will complain.

A nice solution would be to give your class an extension function:

public static bool HasNullProperties(this MyClass x)
{
    return x.Name == null
        && x.Location == null
        && x.OrderSize == null
        ...;
}

public static IEnumerable<MyClass> WhereHasNullProperties(this IEnumerable<MyClass> source)
{
    return source.Where(item => item.HasNullProperties();
}

Usage somewhere in a LINQ statement

var result = dbContext.MyItems.WhereHasNullProperties()
    .GroupBy(...)
    .Select(...);

If you want a full proof solution that works on several classes, consider designing an interface:

interface IHasNullProperties
{
    bool HasNullProperties {get;}
}

Your LINQ function will be:

 public static IEnumerable<TSource> WhereHasNullProperties<TSource>(
      this IEnumerable<TSource> source)
      where TSource : IHasNullProperties
{
    return source.Where(item => item.HasNullProperties();
}

Finally a relatively slow method would be to use reflection: for any class, get all its get-properties that are nullable and see if any of them has a null value:

static bool HasNullPrperties<TSource>(this TSource source)
    where TSource : class
{
     // Take the type of the source, and get all properties of this type
     var result = source.GetType().GetProperties()

         // keep only the readable properties (so you can do GetValue)
         // and those properties that have a nullable type
         .Where(property => property.CanRead 
             && Nullable.GetUnderlyingType(property.Type) != null)

         // for every of this properties, ask the source object for the property value:
        .Select(property => property.GetValue(source))

        // and keep only the properties that have a null value
        .Where(value => value == null);

        // return true if source has any property with a null value
        // = if there is any value left in my sequence
        .Any();
    return result;  
}

Related Articles