无法反序列化列表<t>&#39;来自BsonType&#39; Document&#39;

时间:2016-12-08 06:41:48

标签: c# mongodb mongodb-.net-driver

我在我的C#MVC应用程序中使用MongoDB Driver 2.3.0并收到此错误:

  

&#34;无法反序列化&#39;列表&#39;来自BsonType&#39; Document&#39;&#34;

更新后获取数据 以下是我的代码:

IMongoCollection<Model> objModel = dbHelper.GetCollection<Model>(Model.CollectionName);

var query = Builders<Model>.Filter.And(
    Builders<Model>.Filter.Eq("_id", new ObjectId(Id)),
    Builders<Model>.Filter.Eq("locations.locationid", objLocation.locationid));
var update = Builders<Model>.Update.Set("locations", objLocation.ToBsonDocument());
var result = objModel.UpdateMany(query, update, new UpdateOptions { IsUpsert = true });

if (objLocation.isdefaultlocation)
{
    var lList = (from e in objModel.AsQueryable<Model>()
                 where e._id == ObjectId.Parse(Id)
                 select e.locations).FirstOrDefault();

    lList.Where(i => i.isdefaultlocation == true && i.locationid != objLocation.locationid).ToList()
         .ForEach(s => s.isdefaultlocation = false);

    Model onjO = new Model();
    onjO.locations = lList;
    var query1 = Builders<Model>.Filter.Eq("_id", new ObjectId(Id));
    var update1 = Builders<Model>.Update.Set("locations", MongoDB.Bson.BsonArray.Create(onjO.ToBsonDocument()["locations"]));
    objModel.UpdateMany(query1, update1);
}

return (result.ModifiedCount > 0);

我能够更新记录但是当我尝试使用Linq查询获取记录时,我得到上面提到的错误。下面是我收到错误的代码:

var lList = (from e in objModel.AsQueryable<Model>()
                     where e._id == ObjectId.Parse(Id)
                     select e.locations).FirstOrDefault();

任何帮助都将不胜感激。

0 个答案:

没有答案