0

As I understood, we should use IQueryable to fetch list of data from database server.

Especially when we filter the data based on some condition.

For example below the filter will be done on SQL Server instead of the application to reduce network traffic

var users = _dbContext.users
    .Where(x => x.age > 18)
    .AsQueryable();

But if we did it like below the SQL server will return the whole users table and the where will be executed from the memory list

var users = _dbContext.users
    .Where(x => x.age > 18)
    .ToList();

But my question is what if we actually want the whole table? so does using

var users = _dbContext.users
    .AsQueryable();

has any advantages over using

var users = _dbContext.users
   .Where(x => x.age > 18)
   .ToList();
Uwe Keim
  • 38,279
  • 56
  • 171
  • 280
Jack
  • 83
  • 6
  • `AsQueryable()` is for when you have something that is _not_ `IQueryable`, but want it to behave like it. as far as i understand it, your calls to `AsQueryable()` here don't do anything, because you are calling it on objects that _are_ `IQueryable`.. [see also](https://docs.microsoft.com/en-us/dotnet/api/system.linq.queryable.asqueryable?view=net-5.0): `If the type of source implements IQueryable, AsQueryable(IEnumerable) returns it directly` – Franz Gleichmann Sep 08 '21 at 18:16
  • Can you give an example of something that is not IQueryable? – Jack Sep 08 '21 at 18:21
  • a `List`. for example. that's only `IEnumerable`. – Franz Gleichmann Sep 08 '21 at 18:21

0 Answers0