c# - 如何以不同的方式从同一模型中获取数据以解析到 Newtonsoft?
问题描述
使用 C# .Net5
我试图找出是否有一种方法可以使模型像这样:
public class CollectionOptionModel
{
public string sport { get; set; }
public string plan { get; set; }
public int fromDay { get; set; }
public int fromMonth { get; set; }
public int fromYear { get; set; }
public int toDay { get; set; }
public int toMonth { get; set; }
public int toYear { get; set; }
public int? eventId { get; set; }
public string eventName { get; set; }
public ObservableCollection<TypeModel> marketTypesCollection { get; set; }
public ObservableCollection<TypeModel> countriesCollection { get; set; }
public ObservableCollection<TypeModel> fileTypeCollection { get; set; }
}
问题是我还需要不同格式的相同数据发送到 API,因为我只希望集合在由 newton soft 序列化时返回一个值数组,如下所示:
public class CollectionOptionDownloadModel
{
public string sport { get; set; }
public string plan { get; set; }
public int fromDay { get; set; }
public int fromMonth { get; set; }
public int fromYear { get; set; }
public int toDay { get; set; }
public int toMonth { get; set; }
public int toYear { get; set; }
public int? eventId { get; set; }
public string eventName { get; set; }
public List<string> marketTypesCollection { get; set; }
public List<string> countriesCollection { get; set; }
public List<string> fileTypeCollection { get; set; }
}
并且返回数据需要与通过文件名列表传递到 API 的每个变体相关联,如下所示:
public class SingleOptionResultsModel
{
public string sport { get; set; }
public string plan { get; set; }
public int fromDay { get; set; }
public int fromMonth { get; set; }
public int fromYear { get; set; }
public int toDay { get; set; }
public int toMonth { get; set; }
public int toYear { get; set; }
public int? eventId { get; set; }
public string eventName { get; set; }
public string marketTypesCollection { get; set; }
public string countriesCollection { get; set; }
public string fileTypeCollection { get; set; }
public List<string> fileList { get; set; }
}
这按原样工作,但我必须做一些 for 循环才能将数据复制到每个模型。有没有办法使用自定义 getter 和 setter 以更好的方式实现这一目标?由于我是编码新手,我知道我错过了一些技巧,而且看起来会有很多这样有用的实例,我想我会征求一些建议。
解决方案
推荐阅读
- java - Java 正则表达式是否支持排序序列?
- c++ - 你能专门化 std::unique_lock
- xslt - 有条件地修改多个同名子项中的文本
- javascript - Javascript:通过 ajax 传递创建的图像。
- .net - 暂停 GACUTIL.exe 列表
- javascript - 使用 d3.nest 实现 D3 树图 json
- python - 如何使用exec根据Python3中的字典将列表分配给变量?
- python - 如何将主题公开给 django 中的所有用户?
- python - 将生成器中的项目附加到列表
- c++ - C ++:使用指向大型静态数组的指针安全地初始化一个类