使用Linq时,枚举不能反序列化 [英] Enum can't be deserialized when using Linq

查看:187
本文介绍了使用Linq时,枚举不能反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个枚举

public enum JobStatus
{
    Created = 0,
    Assigning = 1,
    Assigned = 2,
    Started = 3,
    Finished = 4
}

在此界面中

public interface IJob
{
    Guid Id { get; set; }
    JobStatus Status { get; set; }
    bool IsFaulted { get; set; }
    string ErrorMessage { get; set; }
}

我在数据库中输入这些人之一,这样做很顺利。这是什么样子。

I plop one of these guys in the database, and it goes fine. This is what that looks like.

{ "_id" : { "$uuid" : "4e5002b6-3c80-b497-4a33-46b0ea5a39bf"} 
, "_t" : "MyJobObject" 
, "Status" : 0 
, "IsFaulted" : false 
, "ErrorMessage" :  null  
, "Location" : "overthere"}

然后当我尝试用这个代码抓住一个



Then when I try and grab one with this code

Database.GetCollection<IJob>("Jobs").AsQueryable().FirstOrDefault(x=>x.Status == JobStatus.Created);

抛出异常

Unable to determine the serialization information for the expression: jobs.Status.
   at MongoDB.Driver.Linq.Utils.BsonSerializationInfoFinder.GetSerializationInfo(Expression node, Dictionary`2 serializationInfoCache)
   at MongoDB.Driver.Linq.Utils.BsonSerializationInfoHelper.GetSerializationInfo(Expression node)
   at MongoDB.Driver.Linq.PredicateTranslator.BuildComparisonQuery(Expression variableExpression, ExpressionType operatorType, ConstantExpression constantExpression)
   at MongoDB.Driver.Linq.PredicateTranslator.BuildComparisonQuery(BinaryExpression binaryExpression)
   at MongoDB.Driver.Linq.PredicateTranslator.BuildQuery(Expression expression)
   at MongoDB.Driver.Linq.SelectQuery.BuildQuery()
   at MongoDB.Driver.Linq.SelectQuery.Execute()
   at MongoDB.Driver.Linq.MongoQueryProvider.Execute(Expression expression)
   at MongoDB.Driver.Linq.MongoQueryProvider.Execute[TResult](Expression expression)
   at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)

我的首先,我没有将枚举分配给整数,并产生相同的结果。我在两个不同的机器上看到这两个,两个新安装的MongoDb,一个在Windows上,一个在Ubuntu 12.04上。

My first go I didn't have the enums assigned to integers, and that produced the same result. I'm seeing this on two different machines, and two fresh installs of MongoDb, one on windows and one on Ubuntu 12.04.

不知道该怎么做,任何想法?

Not sure what to do about this, any ideas?

推荐答案

我结束了

Database.GetCollection< IJob>(Jobs)。AsQueryable()。ToEnumerable()。FirstOrDefault(x => x.Status == JobStatus.Created);

它的工作正常,虽然我有点担心,它可能会拉下所有的数据在那一点。它似乎没有变慢,而且我一度有> 50k的记录,你会认为如果发生这种情况,你会看到性能下降。

and it works fine, though I'm a little worried that it may be pulling all the data down at that point. It doesn't appear any slower though, and I had >50k records at one point, and you'd think that you'd see a performance degradation if that's what was happening.

这篇关于使用Linq时,枚举不能反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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