Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What to return from an IQuerable when i dont 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 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();
        

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading