gpt4 book ai didi

c# - 如何使用 mongo c# 驱动程序更新数组/列表中的项目?

转载 作者:IT老高 更新时间:2023-10-28 13:26:45 26 4
gpt4 key购买 nike

最近更新到 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

但我得到了这个异常(exception):

Unable to determine the serialization information for the expression: (Album x) => x.Ratings.get_Item(WebApp.Areas.API.Controllers.RatingController+<>c__DisplayClass5.index).Number.

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

最佳答案

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

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

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

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

关于c# - 如何使用 mongo c# 驱动程序更新数组/列表中的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12803563/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com