【问题标题】:MongoDb C# driver throwing Serialization error in DateTime comparisonMongoDb C# 驱动程序在 DateTime 比较中引发序列化错误
【发布时间】:2015-11-06 21:50:39
【问题描述】:

我正在为 MongoDb 使用 C# 驱动程序。我有一个非常简单的“查找”查询:

var cursor = PortalContext.Users.Find(user => DateTime.Now > user.UpdatedDate);
return cursor.ToListAsync();

抛出异常:

Message: "An error has occurred."
ExceptionMessage: "Unsupported filter: (8/13/2015 12:03:44 PM > Serialization(UpdatedDate))."
ExceptionType: "System.ArgumentException"
StackTrace: " at MongoDB.Driver.Linq.Translators.PredicateTranslator.BuildFilter(Expression expression) at ...

【问题讨论】:

  • 您可以尝试将user.UpdatedDate 设置为变量updatedDate 并在Find 表达式中使用它吗?
  • 我的意思是,PortalContext.Users.Find(user => DateTime.Now > updatedDate)

标签: c# mongodb datetime comparison


【解决方案1】:

尝试重新排列您的 Find,使常量 (DateTime.Now) 位于右侧,这样行吗?

var cursor = PortalContext.Users.Find(user => user.UpdatedDate < DateTime.Now);
return cursor.ToListAsync();

1.x 驱动程序 (CSHARP-431) 中存在一个错误,驱动程序没有处理常量位于左侧的这种情况。它很久以前就修复了,但这个问题可能是一个回归。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多