ReSharper
 
Get ReSharper
Get your hands on the new features ahead of the release by joining the Early Access Program for ReSharper 2025.1! Learn more

Code inspection: IQueryable is possibly unintentionally used as IEnumerable

Last modified: 08 April 2024

Objects that implement the IQueryable interface are meant to be processed by LINQ providers (normally, remote databases) with their specific implementations of this interface.

However, as IQueryable is inherited from IEnumerable, there could be cases when an IQueryable object is passed and processed as an IEnumerable object, meaning that all processing is done on the local machine. Such implicit conversions are not a problem in test environments, but could make a huge performance impact in production.

Suppose that you get an IQueryable object from a database:

... and somewhere in your codebase there is a handy extension method for filtering out null items:

Now, let's see how we can use our queryable object:

ReSharper will detect such a conversion and suggest a fix to make it explicit:

Another example is method groups or expression trees, which should not be passed as 'Expression' objects in this case, and therefore trigger the same inspection. The same implicit conversion to IEnumerable happens here and ReSharper suggests adding the explicit .AsEnumerable() to the call: