I have the following function in my base repository
public IEnumerable<T> GetAll(ClaimsPrincipal user)
{
return client.CreateDocumentQuery<T>(collectionLink)
.Where(doc => doc.Type == DocType)
.AsEnumerable()
.Where(doc =>
doc.Owner == user.Id()
|| user.InGroup(doc.Owner)
|| doc.Public);
}
I have two extension methods on the ClaimsPrincipal class. Id()
just returns .FindFirst("user_id").Value
and .InGroup(<id>)
checks if the user has a group-membership to the group that owns the document.
However, am I correct in assuming that once I call .AsEnumerable()
the query goes to the database with only the first where-clause, returns everything it matches, and then does the second where-clause on the client side?
However, am I correct in assuming that once I call .AsEnumerable() the query goes to the database
Not quite.
When this method returns, it won't have hit the database at all. AsEnumerable()
doesn't force the query to execute - it's basically just a cast to IEnumerable<T>
.
You're right in saying the first Where
is performed at the database and the second Where
is performed client-side, but "returns everything it matches" suggests that's done in bulk - which it might be, but it may be streamed as well. The client-side Where
will certainly stream, but whether the IQueryable<T>
implementation fetches everything eagerly is an implementation detail.
If you're really only interested in which filters are in the database and which are local though, you're correct.
See more on this question at Stackoverflow