简体   繁体   English

如何让 C# Mongo 驱动程序使用 Linq 表达式比较 DateTime 字段?

[英]How do I get C# Mongo driver to compare a DateTime field using Linq expressions?

I am creating a select query to pass to MongoDB using Linq Expressions.我正在创建一个 select 查询以使用 Linq 表达式传递给 MongoDB。

I want to query the collection for documents with a DateTimePosed < a given date.我想查询集合中包含DateTimePosed < 给定日期的文档。 (The repository variable is an IQueryable derived from IMongoCollection.AsQueryable() ): repository变量是从IMongoCollection.AsQueryable()派生的IQueryable ):

var parameter = Expression.Parameter(typeof(T), "x");
var finalExpression = Expression.Lambda<Func<T, bool>>(Expression.Constant(true), parameter);
finalExpression.Apply(Expression.LessThan(SomeBusinessObject.DateTimePosted, new DateTime(1988, 2, 1)));
var query = repository.Query;
var results = query.Select(o => o.OfferId.Value).ToList();

When I run the code, the results are empty.当我运行代码时,结果是空的。 When debugging, I see the generated query looks like this:调试时,我看到生成的查询如下所示:

aggregate([{
    "$match" : { "DateTimePosted" : { "$lt" : "1988-02-01T00:00:00-06:00" } }
}])

When I run this in a mongo client, it returns 0:当我在 mongo 客户端中运行它时,它返回 0:

db.getCollection('somecollection').aggregate([{
    "$match" : { "DateTimePosted" : { "$lt" : "1988-02-01T00:00:00-06:00" } }
}]);

But if I modify the query like so:但是,如果我像这样修改查询:

db.getCollection('somecollection').aggregate([{
    "$match" : { "DateTimePosted" : { "$lt" : ISODate("1988-02-01T00:00:00-06:00") } }
}]);

it returns the results.它返回结果。

Can you please tell me what I am doing wrong?你能告诉我我做错了什么吗?

I figured out what was wrong: we are saving the date-time values as strings.我发现出了什么问题:我们将日期时间值保存为字符串。

I had to pass in a valid date-time value as a string in order to get the comparison to work.我必须将有效的日期时间值作为字符串传递,才能使比较生效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Mongo.Driver.Linq 和 Mongo C# 驱动程序 2.3 返回带有过滤子文档的文档? - How do I return a document with filtered sub-documents using Mongo.Driver.Linq with the Mongo C# driver 2.3? MongoDB C#驱动程序:如何确保在数组内容上使用LINQ表达式的索引? - MongoDB C# Driver: How do I ensure an index using LINQ expressions on the contents of an array? 使用c#driver从mongo collection获取DateTime - Get DateTime from mongo collection using c# driver 如何使用 Linq c# 从 SQL Server 数据库中获取短日期时间并与 DateTime.Now 进行比较 - How to get a short DateTime from SQL Server database and compare with DateTime.Now using Linq c# 在 C# 中,使用 Mongo 驱动程序,如何设置“最后修改字段”? - In C#, using the Mongo Driver, how can I set a "last modified field"? 如何使用Linq将SQLite日期时间与C#字符串进行比较 - How to compare SQLite datetime to C# string using Linq &gt; 或 &lt; 使用 C# 驱动程序在 Mongo db 中存储为字符串的日期时间字段上的运算符 - > or< operator on datetime field stored as string in Mongo db using C# driver 如何使用mongo db c#驱动程序执行“和”查询? - How to do “And” queries using mongo db c# driver? 如何使用Mongo C#驱动程序的BSONClassMap将所有属性序列化和反序列化为String - How do I Serialize and DeSerialize all properties to String using Mongo C# Driver's BSONClassMap 如何通过 DateTime 的 C# 驱动程序在 Mongo 集合中查找条目? - How to find an entry in a Mongo Collection through C# driver by DateTime?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM