Enum can't be deserialized when using Linq

回眸只為那壹抹淺笑 提交于 2020-01-15 10:33:55

问题


So I have this enum

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

In this interface

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);

That throws an exception

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)

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?


回答1:


I ended up doing

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

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.



来源:https://stackoverflow.com/questions/15957582/enum-cant-be-deserialized-when-using-linq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!