Home > Software engineering >  What to return from an IQuerable when i dont have something to filter?
What to return from an IQuerable when i dont have something to filter?

Time:09-29

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 is null it means that i dont have something to filter in my query what to return then? I tried this way but it doesnt work

Enumerable.Empty<CustItemTrades>().AsQueryable();
        

CodePudding user response:

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.

  • Related