是不是可以在使用C#添加字段和数值到文档时定义字段类型?

huangapple go评论45阅读模式
英文:

Is it possible to define field types when adding fields and values to a document with C#?

问题

使用Dictionary表示文档是一种向其添加字段和数值的方法。是否有其他方法,可以同时定义字段类型

例如,可以使用字典:

Dictionary<string, object> data = new Dictionary<string, object>
{
    { "field1", var1 },
    { "field2", var2 },
};

然后通过await doc.SetAsync(data);将字段及其值添加到目标文档。

但是否有一种方法可以定义字段的类型呢?

英文:

Representing a document with a Dictionary is one way to add fields and values to it.
Is there any alternative which would also include defining the Field type?

For example a dictionary can be:

Dictionary&lt;string, object&gt; data = new Dictionary&lt;string, object&gt;
        {
            { &quot;field1&quot;, var1 },
            { &quot;field2&quot;, var2 },
        };

Then by using await doc.SetAsync(data); the fields with their values will be added to the targeted document.

Is there a way though to define the types of the fields?

答案1

得分: 1

你不能在任何编程语言中明确设置类型。字段的类型是由你提供的数据的类型确定的(在你的情况下是var1var2)。如果你想确保字段的类型,那么就由你提前转换值。例如,如果你有一个字符串,但你想要一个数字字段,你需要在将该字符串提供给Firestore之前将其转换为数字。

英文:

You cannot set types explicitly in any language. They types of any fields come from the types of the data you provide (in your case, var1 and var2. If you want to ensure the type of any field, then it's up to you to convert the values ahead of time. For example, if you have a string, but you want a number field out of it, you will need to convert that string to a number before you give it to Firestore.

huangapple
  • 本文由 发表于 2023年2月10日 05:56:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404839.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定