I'm new and this is probably very trivial but I have the following method which works fine, using LINQ for MongoDB:
public T Single<T>(System.Linq.Expressions.Expression<Func<T, bool>> whereExpression) where T : class, new()
{
T retval = default(T);
using (var db = Mongo.Create(_connectionString))
{
retval = db.GetCollection<T>().AsQueryable()
.Where(whereExpression).SingleOrDefault();
}
return retval;
}
But I would like to add a "Select" to it (for projection) using a parameter as well, something that might look like this (which obviously doesn't work):
public T SingleWithSelect<T>(System.Linq.Expressions.Expression<Func<T, bool>> whereExpression, System.Linq.Expressions.Expression<Func<T, bool>> selectExpression) where T : class, new()
{
T retval = default(T);
using (var db = Mongo.Create(_connectionString))
{
retval = db.GetCollection<T>().AsQueryable()
.Where(whereExpression)
.Select(selectExpression)
.SingleOrDefault();
}
return retval;
}
In the hopes that it'll return something similar to the following:
var results = db.GetCollection<Entity>("Entities").AsQueryable()
.Where(i => i.Id == someId)
.Select(y => new { y.SomeEntity }).SingleOrDefault();
Essentially I just need to know how to pass the SELECT parameter into the return function - surprisingly hard to find the solution online when so unfamiliar with LINQ.
Thank you.