score:17

Accepted answer

you can refactor the code to not need an empty set by only ever unioning results that you have:

public static iqueryable<t> applysearch<t>(this iqueryable<t> queryable, searchmodel search) where t : class 
{
    var subqueries = new list<iqueryable<t>>();
    if (search != null)
    {
        if (search.policynumber.hasvalue && typeof (ipolicynumber).isassignablefrom(queryable.elementtype))
        {
            subqueries.add(queryable.searchbypolicynumber(search));
        }

        if (search.uniqueid.hasvalue && typeof (iuniqueid).isassignablefrom(queryable.elementtype))
        {
            subqueries.add(queryable.searchbyuniqueid(search));
        }

        if (!string.isnullorwhitespace(search.postcode) && typeof(ipostcode).isassignablefrom(queryable.elementtype))
        {
            subqueries.add(queryable.searchbypostcode(search));
        }
    }

    return subqueries.defaultifempty(queryable)
        .aggregate((a, b) => a.union(b));
}

score:1

temporary hack that i have used

is to change from

var results = enumerable.empty<t>().asqueryable();

to

var results = queryable.where(o => false);

Related Query

More Query from same tag