我在MongoDB DAL类中设置了方法.
public IQueryable<MyModel> Retrieve(Expression<Func<MyModel, bool>> expression)
{
if (!BsonClassMap.IsClassMapRegistered(typeof(MyModel)))
{
DoMapping();
}
var client = new MongoClient(MongoConnectionString);
var database = client.GetDatabase("DatabaseName");
var documents = database.GetCollection<MyModel>("MyModelTable");
return documents.AsQueryable<MyModel>().Where(expression);
}
我想做一些简单的事情
var result = Retrieve(a => a.someDateProperty.Date >= startDate && a.someDateProperty.Date <= endDate);
An exception of
type ‘system.invalidOperationException’ occurred in MongoDB.Driver.dll
but was not handled in user codeAdditional information: {document}{SomeDateProperty}.Date is not
supported.
我正在使用官方的C#驱动程序版本2.2.4.26.
有没有办法只查询日期?我已经看到了有关使用DbFunctions.Truncate的帖子,但是那是在EntityFramework库中,我希望远离.
解决方法:
我在使用Date时遇到了同样的问题,我使用了:
想法是使用两个边框(DateTime.Date也是DateTime,但带有0小时0分钟…,第二个是第二天,也带有0小时0分钟…).
DateTime currentDayStart = DateTime.Now.Date;
DateTime currentDayEnds = DateTime.Now.Date.AddDays(1);
var result = Retrieve(a => a.someDateProperty >= currentDayStart && a.someDateProperty < currentDayEnds);
它对我来说很好.