Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 存储包含列表的自定义对象<&燃气轮机;在数据库中_C#_Android_List_Object_Xamarin - Fatal编程技术网

C# 存储包含列表的自定义对象<&燃气轮机;在数据库中

C# 存储包含列表的自定义对象<&燃气轮机;在数据库中,c#,android,list,object,xamarin,C#,Android,List,Object,Xamarin,我想将一个类/对象保存到Android中的本地SQLite数据库中。但是这个对象包含一个列表。据我所知,无法将列表保存到数据库中,因此我希望忽略它,并在从数据库加载对象后填充它 我一直在看关于Android数据库的多个教程,但都没有介绍如何处理无法存储在数据库中的属性 我还查看了xamarin文档,并尝试使用[ignore],但没有成功 这是我想要保存到数据库中的类,并且[ignore]仍然存在: [Table("Room")] public class Room { [Primary

我想将一个类/对象保存到Android中的本地SQLite数据库中。但是这个对象包含一个列表。据我所知,无法将列表保存到数据库中,因此我希望忽略它,并在从数据库加载对象后填充它

我一直在看关于Android数据库的多个教程,但都没有介绍如何处理无法存储在数据库中的属性

我还查看了xamarin文档,并尝试使用[ignore],但没有成功

这是我想要保存到数据库中的类,并且[ignore]仍然存在:

[Table("Room")]
public class Room
{
    [PrimaryKey, AutoIncrement]
    public int id { get; set; }

    public string RoomName { get; set; }

    public string NFC_UId { get; set; }

    [Ignore]
    public List<Switch> RoomSwitches = new List<Switch>();

    public Room()
    {

    }
}
[桌子(“房间”)]
公共教室
{
[主密钥,自动增量]
公共int id{get;set;}
公共字符串RoomName{get;set;}
公共字符串NFC_UId{get;set;}
[忽略]
public List RoomSwitches=new List();
公共房间()
{
}
}
尝试使用“忽略”时出现的错误是:

严重性代码说明项目文件行抑制状态 错误CS0592属性“忽略”对此声明类型无效。它仅对“属性,索引器”声明有效


那么,是否有可能忽略该列表,或者我的问题是否有其他解决方案?

如果您使用的是来自
android架构的Room
,那么您可以向数据库中添加
类型转换器
,类似于@Wheels73在
GSON
的帮助下提出的建议。例如,您可以将类型转换器编写为:

class ListTypeConverter {

    @TypeConverter fun fromStringToList(stringValue: String?) : List<String>? {
        val listType = object: TypeToken<List<String>>(){}.type
        return if(stringValue?.isNotEmpty == true) {
            Gson().fromJson(stringValue, listType)
        } else null
    }

    @TypeConverter fun fromListToString(listValue: List<String>?) : String? =
        Gson().toJson(listValue ?: return null)

}
如果您没有使用
房间
。与此类似,使用
GSON
并将对象转换为
String
,以创建列表并将其保存为字段。我认为它在C#中也会起到类似的作用。我不太熟悉,但也许你可以

var listString=JsonConvert.SerializeObject(ListofObject类型)


var listofoobject=JsonConvert.DeserializeObject(listString)

结果是我不得不像这样使用[ignore]:

[Table("Room")]
public class Room
{
    [PrimaryKey, AutoIncrement]
    public int id { get; set; }

    public string RoomName { get; set; }

    public string NFC_UId { get; set; }

    [Ignore]
    public List<Switch> RoomSwitches { get; set; } //note the get and set instead of new list

    public Room()
    {

    }

    public getswitches(){} <---
}
[桌子(“房间”)]
公共教室
{
[主密钥,自动增量]
公共int id{get;set;}
公共字符串RoomName{get;set;}
公共字符串NFC_UId{get;set;}
[忽略]
public List RoomSwitches{get;set;}//注意get和set,而不是新列表
公共房间()
{
}

公共GetSwitchs(){}我自己没有使用过android,但您是否考虑过将房间对象序列化为Json字符串。然后您可以存储整个对象并恢复其状态。不幸的是,房间是我创建的自定义对象,但您可以尝试将对象序列化为Json字符串。例如:将字段更改为
公共字符串RoomSwitchs
,您可以有一个方法是
public void setRoomSwitches(List)
public List getRoomSwitches()
我发现了如何正确使用[ignore],并找到了另一种解决问题的方法。但是,请向thx寻求帮助
[Table("Room")]
public class Room
{
    [PrimaryKey, AutoIncrement]
    public int id { get; set; }

    public string RoomName { get; set; }

    public string NFC_UId { get; set; }

    [Ignore]
    public List<Switch> RoomSwitches { get; set; } //note the get and set instead of new list

    public Room()
    {

    }

    public getswitches(){} <---
}