Sotiris
Sotiris

Reputation: 38

What to return from an IQueryable when I don't have something to filter?

public static IQueryable<CustItemTrades> SeriesIdFilter(this 
IQueryable<CustItemTrades> trades, List<short?> lastPriceDocs) =>
    lastPriceDocs != null ? trades.Where(c => lastPriceDocs.Contains(c.SeriesId)) :
// what to return here if my List<short> == null

When my List<short?> is null it means that I don't have something to filter in my query. What to return then?

Upvotes: 0

Views: 94

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460238

Your approach does not work because Enumerable.Empty returns nothing, but you want to return all, if i have understood your requirement correctly.

When my List is null it means that i dont have something to filter in my query

If you don't have something to filter, then don't filter.

public static IQueryable<CustItemTrades> SeriesIdFilter(this IQueryable<CustItemTrades> trades, List<short?> lastPriceDocs)
{
    if(lastPriceDocs == null)
    {
        return trades;
    }
    
    return trades.Where(c => lastPriceDocs.Contains(c.SeriesId));
}

Don't modify the query so that the Where matches always, simply return everything.

Upvotes: 4

Related Questions