使用C#驱动程序将UDT元素添加到Cassandra中的列表集合的正确方法是什么? [英] What is the proper way to add a UDT element to a list collection in Cassandra using c# driver?

查看:71
本文介绍了使用C#驱动程序将UDT元素添加到Cassandra中的列表集合的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UDT列表,例如:

I have a list of UDT, something like:

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

在我的客户代码中,我想将一个元素附加到东西上。理想情况下,我想执行以下操作(或类似操作):

In my client code, I'd like to append an element to "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


$ b中不支持绑定变量$ b

我可以通过将mytype转换为json来使其工作,例如:

I can get this to work by converting mytype to json, with something like:

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

但是,要知道如何将对象转换为json很难(例如,不要引用带双引号的字符,则需要用单引号将其引起来)。

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).

因此,我希望有更好的方法将类型元素添加到列表中。

As such, I am hoping there is a better way to add a type element to a list.

感谢您的帮助,
Eric

Thanks for your help, Eric

推荐答案

正确的CQL语法为:

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

它期望参数类型为 list< MyType> 。在您的情况下,将是:

It expects a parameter of type list<MyType>. In your case, it would be:

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

这篇关于使用C#驱动程序将UDT元素添加到Cassandra中的列表集合的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆