我在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);
它对我来说很好.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。