c# - 如何将包含另一个列表的列表保存到文件中
问题描述
所以我有我的用户对象:
public class UserModel
{
public int Id { get; set; }
public string Username { get; set; }
public List<ProjectModel> Projects { get; set; }
}
如您所见,我有一个属于用户的项目列表。为了拯救我的用户,我做了这个功能:
public void AddNewUser(UserModel user)
{
try
{
string folder = @"c:\temp\";
StreamWriter sw = new StreamWriter($"{folder}input.txt", append: true);
sw.WriteLine($"{user.Id}," +
$"{user.Username}");
sw.Close();
}
catch (Exception e)
{
Console.WriteLine($"The file could not be read: {e.Message}");
}
}
但是现在我有这个问题,如何保存项目列表并关联到用户。我知道我可以将每个项目保存到另一个文件或保存到同一个文件,但我怎样才能将它们关联起来?
解决方案
推荐阅读
- swift - 在三角形 CAShapeLayer 上方添加文本
- javascript - 使用异步导致:错误 [ERR_REQUIRE_ESM]: Must use import to load ES Module asyncToGenerator.js
- python - 单击提交付款按钮后,Django Stripe 付款响应无效参数
- mysql - 外键可以引用多个表吗?
- javascript - 在填写表格之前隐藏文章的其余部分
- php - php说每天都有不同的时区秒数
- go - 使用无服务框架在 API 模式下本地调试 lambda golang
- arrays - 一种在 C 中知道结构数组中的元素是否为空/NULL 的方法
- python-3.x - Python根据条件获取类列表的索引
- c - Gcc 无法识别“双复数”减速