I am using the Net Driver to manage a data across a number of Collections in a MongoDB database and prefer to LINQ queries.
The general pattern seems to be of the form:
var result = collection.AsQueryable().Where((document) => …).Select((ducument) => …);
My questions are:
-
Is there a significant overhead in making a collection ‘Queryable’ every time it is interrogated in this way?
-
Is the ‘Queryable’ representation of a collection ‘synchronized’ with the collection itself over time?
-
Is the following pattern viable?
public class EntityRepository<TEntity, TKey> … where TKey : ObjectId
{
private IMongoCollection<TEntity>_entityCollection;
private IMongoQueryable<TEntity>_entityQueryableCollection;
public EntityRepository(IMongoCollection<TEntity<TKey>> collection)
{
_entityCollection = collection;
_entityQueryableCollection = collection.AsQueryable();
}
public Task<TEntity<TKey>> FindByIdAsync(TKey id)
{
return _entityQueryableCollection
.Where((document) => document.Id.Equals(id))
.Select((document) => document);
}
}
Any assistance appreciated,
Nico