【问题标题】:What is the proper way to add a UDT element to a list collection in Cassandra using c# driver?使用 c# 驱动程序将 UDT 元素添加到 Cassandra 中的列表集合的正确方法是什么?
【发布时间】:2015-12-01 00:41:29
【问题描述】:

我有一个 UDT 列表,类似于:

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

在我的客户端代码中,我想将一个元素附加到“stuff”。理想情况下,我想做以下(或类似的事情):

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

很遗憾,此操作失败并出现以下错误:

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

我可以通过将 mytype 转换为 json 来实现此功能,例如:

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

但是,知道如何将对象转换为 json 是很棘手的(例如,不是用双引号引用字符,而是需要用单引号引用它们)。

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

感谢您的帮助, 埃里克

【问题讨论】:

    标签: c# cassandra


    【解决方案1】:

    正确的 CQL 语法应该是:

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

    它需要一个list&lt;MyType&gt; 类型的参数。在您的情况下,它将是:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-01
      • 2016-11-09
      • 1970-01-01
      • 2023-02-09
      • 2017-08-22
      • 2019-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多