简体   繁体   English

使用c#驱动程序在Cassandra中将UDT元素添加到列表集合的正确方法是什么?

[英]What is the proper way to add a UDT element to a list collection in Cassandra using c# driver?

I have a list of UDT, something like: 我有一个UDT列表,如:

create table MyTable
{
  ...
  stuff list<frozen<MyType>>,
  ...
}

In my client code, I'd like to append an element to "stuff". 在我的客户端代码中,我想将一个元素附加到“stuff”。 Ideally, I'd like to do the following (or something similar): 理想情况下,我想做以下(或类似的事情):

this.Mapper<MyTable>("SET stuff = stuff + [?] WHERE id = ?", mytype, id);

Unfortunately, this fails with the following error: 不幸的是,这失败并出现以下错误:

Invalid list literal for stuff: bind variables are not supported inside collection literals

I can get this to work by converting mytype to json, with something like: 我可以通过将mytype转换为json来实现这一点,例如:

var stuffAsJson = stuff.ToJson();
var update = string.Format("SET stuff = stuff + [{0}] WHERE id = ?", commentAsJson);
this.Mapper.Update<MyTable>(update, stuffAsJson, id);

However, it was tricky to know how to convert the object to json (for example, instead of quoting characters with double quote, they needed to be quoted with single quote). 但是,知道如何将对象转换为json是很棘手的(例如,不是引用带双引号的字符,而是需要引用单引号)。

As such, I am hoping there is a better way to add a type element to a list. 因此,我希望有更好的方法将类型元素添加到列表中。

Thanks for your help, Eric 谢谢你的帮助,埃里克

The correct CQL syntax would be: 正确的CQL语法是:

UPDATE table1 SET stuff = stuff + ? WHERE id = ?

It expects a parameter of type list<MyType> . 它需要list<MyType>类型的参数。 In your case, it would be: 在你的情况下,它将是:

this.Mapper<MyTable>(
  "SET stuff = stuff + ? WHERE id = ?", new List<MyType> {myItem}, id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM