首页 > 解决方案 > 如何以不同的方式从同一模型中获取数据以解析到 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 以更好的方式实现这一目标?由于我是编码新手,我知道我错过了一些技巧,而且看起来会有很多这样有用的实例,我想我会征求一些建议。

标签: c#model

解决方案


推荐阅读