asp.net-web-api - 如何使用 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
解决方案
用于映射数据的MakeTable
和Table1
类Table2
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 中的查询类型
推荐阅读
- excel - 防止价值重复
- firebase - Flutter Firebase 身份验证 - 注销和登录后生成的新匿名用户
- python - Django“用户”对象没有属性“电话”
- firebase - 存储为整数与字符串大小
- flutter - 无法通过 ScanStreamTransformer 正确检索模型
- postgresql - pgAdmin 中的导入/导出对话框消失
- matlab - 在频谱分析 MATLAB 中查找噪声频率区间
- flutter - 如何在文本小部件中获取 Flutter 列表中项目的编号
- sql - 外键“...”在引用表“...”中引用了无效列“...”。SQL 服务器
- regex - 需要帮助检查我的 CFG 问题是否正确