
There has been a discussion around town about the difference between an IEnumerable and an IQueryable, especially in c# interviews. I won’t be diving into the fact that IEnumerable is part of the System.Collections namespace and IQueryable belongs to System.Linq namespace (or did I???). Rather, Iβll focus on the practical usage of bothβhow they work, and when to use each.
IQueryable
public IActionResult GetPeople()
{
// It will retrieve 2 records from database
IQueryable<Person> people = _context.People.Take(2);
// Note: At the above line, no data will be retrieved from the database
return Ok(people); // Data will be retrieved here
}
Corresponding sql
Note that, I am using Sqlite, the above code is translated to this query:



