首页 > 解决方案 > 如何使用 ASP.NET Core C# 将数据集反序列化为类列表

问题描述

如何在 C# 中将数据集序列化为对象?我的存储过程返回一个数据集,其中包含基于这些选择语句的表:

select 
    st.Active, st.CountryCode, st.StateCode, st.StateName,st.GSTCode  
from 
    StateMaster st

select 
    ct.CountryCode, ct.CountryName, ct.Active 
from 
    CountryMaster ct

select 
    cm.Active, cm.CityCode, cm.CityName, cm.CountryCode,
    cm.CreateDate, cm.ID, cm.StateCode, cm.CreatedBy 
from 
    CityMaster cm 

它返回:

{
    "Table": [
        {
            "CountryCode": "91",
            "StateCode": "01",
            "StateName": "Andhra Pradesh",
            "Active": true,
            "GSTCode": "37"
        },
        {
            "CountryCode": "91",
            "StateCode": "02",
            "StateName": "Arunachal Pradesh",
            "Active": true,
            "GSTCode": "12"
        }
    ],
    "Table1": [
        {
            "CountryCode": "91",
            "CountryName": "India",
            "Active": true
        }
    ],
    "Table2": [
        {
            "ID": 1113,
            "CityCode": "1",
            "CityName": " Alipur ",
            "StateCode": "31",
            "CountryCode": 91,
            "Active": true,
            "CreateDate": "2018-07-25T10:59:32.44",
            "CreatedBy": "6000013"
        },
        {
            "ID": 1122,
            "CityCode": "10",
            "CityName": " Bindraban ",
            "StateCode": "31",
            "CountryCode": 91,
            "Active": true,
            "CreateDate": "2018-07-25T10:59:42.437",
            "CreatedBy": "6000013"
        },
        {
            "ID": 1212,
            "CityCode": "100",
            "CityName": " Bapatla ",
            "StateCode": "01",
            "CountryCode": 91,
            "Active": true,
            "CreateDate": "2018-07-25T11:01:21.817",
            "CreatedBy": "6000013"
        }
    ]
}

JsonConvert.DeserializeObject<AllClsList>(jsondpl);返回一个空类。

请指导我-如何将数据集反序列化或直接转换为AllClsList

标签: asp.net-web-apiasp.net-core

解决方案


用于映射数据的MakeTableTable1Table2 DTO如下:

public class Table
{
   public string CountryCode {get; set;}
   public string StateCode {get; set;}
   public string StateName {get; set;}
   public bool Active {get; set;}
   public string GSTCode {get; set;}
}

public class Table1
{
   public string CountryCode {get; set;}
   public string CountryName {get; set;
   public bool Active {get; set;}
}

public class Table2
{
   public string ID {get; set;}
   public string CityCode {get; set;}
   public string CityName {get; set;}
   public string StateCode {get; set;}
   public string ContryCode {get; set;}
   public bool Active {get; set;}
   public string CreateDate {get; set;}
   public string CreatedBy {get; set;}
}

现在编写一个包含所有表的模型类,如下所示:

public class AllTables
{
   public List<Table> Tables {get; set;}
   public List<Table1> Table1s {get; set;}
   public List<Table2> Table2s {get; set;}
}

现在,如果您通过 API 调用收到 JsonData,则反序列化如下:

var allTables = JsonConvert.DeserializeObject<AllTables>(yourJsonData);

如果返回数据的存储过程在您自己的应用程序数据库中,则执行以下操作:

你的DbContext

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext()
    {
    }
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {

    }

    public DbQuery<AllTables> AllTables { get; set; }
}

然后进行如下查询:

var allTables = context.AllTables
    .FromSql("EXECUTE dbo.YourStoredProcedure")
    .ToList();

有关更多详细信息:EF Core 中的查询类型


推荐阅读