Can AsEnumerable()
be removed from GetCars
, and GetCars
is still a deferred execution?
public IEnumerable<Car> GetCars(string id)
{
IEnumerable<Car> cars = IDocumentClient.CreateDocumentQuery<Car>(link).AsEnumerable();
return cars.Where(r => r.Id == Id);
}
Like below:
//GetCars2 is still deferred execution?
public IEnumerable<Car> GetCars2(string Id)
{
IOrderedQueryable<Car> cars = IDocumentClient.CreateDocumentQuery<Car>(link)
return cars.Where(r => r.Id == .Id);
}
Update
I want all the filterings to be done in database first, thus reduce data returned.
Also, I have more filtering after calling GetCar()
. Using IEnumerable
allows deferred execution for filtering inside and outside of GetCar()
.
Can IQueryable
be used instead? What is the benefit?
Is the filtering in GetCars3 ALSO considered for SQL query (both GetCar2 and GetCar3)? Thus, both method reduce data returned from database?
public IEnumerable<Car> GetCars3(string Model )
{
return GetCars2(id).Where(r => r.Model == Model);
}
Update2
As mjwills
suggests to use IQueryable as return type of GetCarX(). But I don't understand why. Because using IEnumerable<> still has all the benefits. That is, filetering is performed on database side, not C#. Please correct me if I am wrong.
Returning IEnumerable<T> vs. IQueryable<T>
LINQ provider:
https://azure.microsoft.com/en-us/blog/azure-documentdb-s-linq-provider-just-got-better/
https://github.com/Azure/azure-cosmos-dotnet-v2/issues/58
SQL: https://learn.microsoft.com/en-gb/azure/cosmos-db/sql-query-getting-started#linq-to-documentdb-sql
Update 3
This question is related to AsEnumerable(), which is used within the same function that Where
is defined, as an example of don't it
.
It has a subtle difference from previous posts, which is better explicitly explained on this post.
Thus, this post should not be marked as duplicate.