如何使用mongo c#driver更新数组/列表中的项目?

时间:2012-10-09 15:34:44

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

最近更新到10gen c# driver for mongodb我想更新我的代码,以便使用强类型版本。

我之前的电话是:

var update2 = new UpdateBuilder();
var index = album.Ratings.IndexOf(rating);
update2.Set("Ratings." + index + ".Number", number);
update2.Set("Rating", album.Rating);
_session.Db().GetCollection<Album>("Album")
    .Update(Query<Album>.Where(x => x.Id == objId), update2); //this line is working

新的电话会是:

update.Set(x => x.Ratings[index].Number, number);
//update2.Set("Ratings." + index + ".Number", number); previous call

但我得到了这个例外:

  

无法确定表达式的序列化信息:   (专辑x)=&gt;   x.Ratings.get_Item(WebApp.Areas.API.Controllers.RatingController + LT;&GT; c__DisplayClass5.index)。。数

有什么方法可以更新List中的项目吗?

1 个答案:

答案 0 :(得分:8)

有趣的问题。这在使用如下常量时起作用:

var update = Update<Album>.Set(x => x.Ratings[0].Number, 10);

然而,当您使用变量时,这显然会中断,就像您使用索引一样。这绝对是个错误。我在这里为它创建了一个Jira问题:https://jira.mongodb.org/browse/CSHARP-598

这很可能是由于我们在处理表达式之前没有对其进行部分评估。

相关问题