return _context.Users
simply returns an object of type DbSet<Users>
. It doesn't iterate over it or do any work it's just passing an object that allows you access to database data.
DbSet<T>
is also an IQueryable<T>
, which means the database isn't called until you call some sort of executing function like .ToList()
or .Single(x=>x.Id == idToLookFor)
If you were to iterate over it asynchronously then you would have an async Get() method, ex;
return await _context.Users.ToListAsync()
Update
I realized that actually didn't answer your question,
It's very unlikely that you would ever want to return your entire table. (SELECT * FROM [Users]) So what the Get()
method here is an anti-pattern (in my opinion**) known as 'exposing an IQueryable'
So in your controller you can do something like
_context.Get().Where(user=>user.FirstName == 'Steve').ToList()
or you could make it async as you figure you should be doing on a database call
await _context.Get().Where(user=>user.FirstName == 'Steve').ToListAsync()
So, is the template generated Get()
an error? No, but I have an opinion that you shouldn't be exposing an IQueryable as a public method, so I disagree with it.
IQueryable<T>
var query = _context.Users; //SQL: * FROM [Users]
query = query.Where(x=>x.Name == "Steve");
//SQL: * FROM [Users] WHERE Name = 'Steve'
query = query.Where(x=>x.wearsHats == true);
//SQL: * FROM [Users] WHERE Name = 'Steve' AND WearsHats = true
query = query.Select(x=>x.Name);
//SQL: Name FROM [Users] WHERE Name = 'Steve' AND WearsHats = true
var result = query.ToList()
//SQL: SELECT Name FROM [Users] WHERE Name = 'Steve' AND WearsHats = true