无法使用.Date确定* expression *的序列化信息 [英] Unable to determine the serialization information for *expression* using .Date

查看:237
本文介绍了无法使用.Date确定* expression *的序列化信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从带日期过滤器的mongoDB集合中读取数据(仅应忽略日期和时间):

I'm trying to read from a mongoDB collection with a Date filter (only the Date, time should be ignored):

var filterDefinition = Builders<SomeClass>.Filter.Eq(p => p.SomeDateTimeProperty.Date, DateTime.Now.Date);
using (var cursor = await _someCollection.FindAsync(filterDefinition))
{ ... }

SomeClassSomeDateTimeProperty属性如下所示:

[BsonIgnoreExtraElements]
[Serializable]
public class ReportConfiguration
{
    ...
    public DateTime SomeDateTimeProperty { get; set; }
    ...
}

尝试执行.FindAsync()时,代码将引发InvalidOperationException:

The code throws an InvalidOperationException when trying to .FindAsync():

类型为"System.InvalidOperationException"的异常发生在 MongoDB.Driver.dll,但未在用户代码中处理 信息:无法确定p的序列化信息 => p.SomeDateTimeProperty.Date

An exception of type 'System.InvalidOperationException' occurred in MongoDB.Driver.dll but was not handled in user code Additional information: Unable to determine the serialization information for p => p.SomeDateTimeProperty.Date

如果我删除过滤器中的.Date部分(p.SomeDateTimeProperty.Date,DateTime.Now.Date),一切都会起作用,但是无论hh \ mm是什么,我都需要yyyy \ mm \ dd比较.

Everything works if I remove the .Date part in the filter (p.SomeDateTimeProperty.Date, DateTime.Now.Date), but I need a yyyy\mm\dd comparison regardless of the hh\mm.

Stacktrace说:

Stacktrace says:

at MongoDB.Driver.ExpressionFieldDefinition`2.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Driver.SimpleFilterDefinition`2.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Driver.MongoCollectionImpl`1.CreateFindOperation[TProjection](FilterDefinition`1 filter, FindOptions`2 options)
at MongoDB.Driver.MongoCollectionImpl`1.FindAsync[TProjection](FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken)
at MongoDB.Driver.IMongoCollectionExtensions.FindAsync[TDocument](IMongoCollection`1 collection, FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken)
at [...]

可能是什么问题?

推荐答案

您是否考虑过使用Lt和Gt运算符使用between子句

Have you thought about using a between clause using Lt and Gt operators

 var b = Builders<SomeClass>.Filter;
 var date = DateTime.UtcNow.Date;
 var filter = b.And(
        b.Gte(x => x.SomeDateTimeProperty, date), 
        b.Lt(x => x.SomeDateTimeProperty, date.AddDays(1))
        );
 ...

这篇关于无法使用.Date确定* expression *的序列化信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆