Enumerable.Empty<T>() equivalent for IQueryable

C#ReturnIqueryable

C# Problem Overview


When a method returns IEnumerable<T> and I do not have anything to return, we can use Enumerable.Empty<T>().

Is there an equivalent to the above for a method returning IQueryable<T>

C# Solutions


Solution 1 - C#

Maybe:

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

Solution 2 - C#

Enumerable.Empty<T>().AsQueryable(); should do it.

Solution 3 - C#

Try return new T[0].AsQueryable();

Solution 4 - C#

Say you have an IQueryable<T> called result:

return result.Take(0);

Solution 5 - C#

I would advise against alejandrobog's answer as this will still use memory to create an empty array.

Array.Empty<T>().AsQueryable();

or

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

are preferred. Array.Empty will allocate a static typed array so only one empty array of T is created and that is shared amongst all Empty queryables.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNumanView Question on Stackoverflow
Solution 1 - C#SunnyView Answer on Stackoverflow
Solution 2 - C#JoshView Answer on Stackoverflow
Solution 3 - C#alejandrobogView Answer on Stackoverflow
Solution 4 - C#Protector oneView Answer on Stackoverflow
Solution 5 - C#JoshView Answer on Stackoverflow