6

随着最近对 mongodb 的 10gen c# 驱动程序的更新,我想更新我的代码,以便它使用强类型版本。

我之前的电话是:

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

但我得到了这个例外:

无法确定表达式的序列化信息:(Album x) => x.Ratings.get_Item(WebApp.Areas.API.Controllers.RatingController+<>c__DisplayClass5.index).Number。

有什么办法可以更新列表中的项目吗?

4

1 回答 1

8

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

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

However, this apparently breaks when you use a variable, like you have done with index. This is definitely a bug. I have created a Jira issue for it here: https://jira.mongodb.org/browse/CSHARP-598.

This is most likely due to us not partially evaluating the expression before processing it.

于 2012-10-10T18:03:30.587 回答