Enumerable.Empty<T>() equivalent for IQueryable
C#ReturnIqueryableC# 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.