基本上我需要这样的数据类型:
int[] list1 = new int[4] { 1, 2, 3, 4 };
int[] list2 = new int[4] { 5, 6, 7, 8 };
int[] list3 = new int[4] { 1, 3, 2, 1 };
int[] list4 = new int[4] { 5, 4, 3, 2 };
int[][] lists = new int[][] { list1 , list2 , list3 , list4 };
自定义大小的数组数组,包含4个整数。
我可以在Realm数据库中执行此操作吗?
感觉更好,更换&#34;自定义大小的阵列&#34; List<int[4]>
但我怀疑这是可能的。
答案 0 :(得分:3)
有几种方法可以解决这个问题,这里有一个方法。 ; - )
我会创建两个RealmObject
。一个定义数组的单个元素(在我的示例中由四个整数定义的Color
)和一个包含RealmObject
这些数组元素的IList
。
public class Color : RealmObject
{
public int R { get; set; }
public int G { get; set; }
public int B { get; set; }
public int A { get; set; }
public int[] RGBA
{
get { return new int[] { R, G, B, A }; }
set { R = value[0]; G = value[1]; B = value[2]; A = value[3]; }
}
}
public class MaterialColors : RealmObject
{
public string Material { get; set; }
public Color PrimaryColor { get; set; }
public IList<Color> AlternativeColors { get; }
public void AddAlts(Color[] ca)
{
for (int i = 0; i < ca.Length; i++)
{
AlternativeColors.Add(ca[i]);
}
}
}
using (var realm = Realm.GetInstance(new RealmConfiguration { SchemaVersion = 1 }))
{
var primary = new Color { RGBA = new int[] { 1, 2, 3, 4 } };
var alt1 = new Color { RGBA = new int[] { 5, 6, 7, 8 } };
var alt2 = new Color { RGBA = new int[] { 1, 3, 2, 1 } };
var alt3 = new Color { RGBA = new int[] { 5, 4, 3, 2 } };
var material = new MaterialColors
{
Material = "StackOverflow",
PrimaryColor = primary,
};
// Add array element one at a time...
material.AlternativeColors.Add(alt3);
// Add multiple elements (array[]) via custom method...
material.AddAlts(new Color[] { alt1, alt2 });
realm.Write(() =>
{
realm.Add(material);
});
var materials = realm.All<MaterialColors>();
foreach (var aMaterial in materials)
{
Console.WriteLine($"Pri: [{aMaterial.PrimaryColor.RGBA[0]}:{aMaterial.PrimaryColor.RGBA[1]}:{aMaterial.PrimaryColor.RGBA[2]}:{aMaterial.PrimaryColor.RGBA[3]}]");
foreach (var color in aMaterial.AlternativeColors)
{
Console.WriteLine($"Alt: [{color.RGBA[0]}:{color.RGBA[1]}:{color.RGBA[2]}:{color.RGBA[3]}]");
}
}
}
Pri: [1:2:3:4]
Alt: [5:4:3:2]
Alt: [5:6:7:8]
Alt: [1:3:2:1]
答案 1 :(得分:2)
来自@SushiHangover的精彩回答。
我们将在某个时候为此提供更多直接支持。我们不承诺发布日期,但您可以track the issue查看其处理时间和来源。
如果您对方案或所需行为有进一步的设计反馈,请在that issue 1194添加评论。