What is the difference between IEnumerable and IQueryable in C#? Why to use IQueryable for sql queries?

IQueryable inherited from IEnumerable interface only, so anything you can do with a IEnumerable, you can also do with an IQueryable also.

For example, iterating the collection can be done by both IEnumerable and IQueryable.
 
  • IEnumerable is used with in-memory collection.

  • IQueryable is better in getting result from database.

IEnumerable internally uses IEnumerator only to iterate the collection via forach loop.

IEnumerable simplifies the use of IEnumerator.


Post a Comment

Previous Post Next Post