-1

Most of the blogs suggest to use IEnumerable for in-memory collections. Are there any advantages? Can I use IQueryable for in-memory collections also?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Why would you want to use it for in-memory collections? You can convert any `IEnumerable` to `IQueryable` with `AsQueryable` but that only introduces unnecessary overhead since almost all method `IQueryable` also exists for `IEnumerable` – Ackdari Dec 16 '20 at 07:44
  • 2
    If there's a measurable difference, it'll be that IQueryable is slower - because it works in terms of expression trees which means some compilation is postponed until runtime. – Damien_The_Unbeliever Dec 16 '20 at 08:04

2 Answers2

2

Check IQueryable documentation

public interface IQueryable : System.Collections.IEnumerable

IQueryable implements IEnumerable, so you can do whatever you can with IEnumerable and more.

Check the differences here: Returning IEnumerable<T> vs. IQueryable<T>

Basically:

  • IQueryable -> Linq to Sql
  • IEnumerable -> Linq to Objects

If it's not SQL or any other data source (not objects), then you have no reason to use IQueryable.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
0

This is kind of an oversimplification, but IQueryable basically is a marker interface for APIs that translate the IQueryable calls to queries for a particular storage engine, such as a database.

Querying a List<T> or other collection with IQueryable extension methods is not going to turn that into database calls, because the storage isn't a database; it's in application memory and will be executed by the runtime.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272