我有一个UDT列表,如:
create table MyTable
{
...
stuff list<frozen<MyType>>,
...
}
Run Code Online (Sandbox Code Playgroud)
在我的客户端代码中,我想将一个元素附加到"stuff".理想情况下,我想做以下(或类似的事情):
this.Mapper<MyTable>("SET stuff = stuff + [?] WHERE id = ?", mytype, id);
Run Code Online (Sandbox Code Playgroud)
不幸的是,这失败并出现以下错误:
Invalid list literal for stuff: bind variables are not supported inside collection literals
Run Code Online (Sandbox Code Playgroud)
我可以通过将mytype转换为json来实现这一点,例如:
var stuffAsJson = stuff.ToJson();
var update = string.Format("SET stuff = stuff + [{0}] WHERE id = ?", commentAsJson);
this.Mapper.Update<MyTable>(update, stuffAsJson, id);
Run Code Online (Sandbox Code Playgroud)
但是,知道如何将对象转换为json是很棘手的(例如,不是引用带双引号的字符,而是需要引用单引号).
因此,我希望有更好的方法将类型元素添加到列表中.
谢谢你的帮助,埃里克
正确的CQL语法是:
UPDATE table1 SET stuff = stuff + ? WHERE id = ?
Run Code Online (Sandbox Code Playgroud)
它需要一个类型的参数list<MyType>.在你的情况下,它将是:
this.Mapper<MyTable>(
"SET stuff = stuff + ? WHERE id = ?", new List<MyType> {myItem}, id);
Run Code Online (Sandbox Code Playgroud)