【发布时间】:2016-03-11 10:56:20
【问题描述】:
我有数据需要循环遍历并将其转换为带有嵌套数组的 JSOn 格式。 下面是我的数据 现在我看到了执行此过程的两个选项
ClientName Dno DnoId Dfull DfullId
ClientA AB342A 16711 AB342A-J2015 1
ClientB AB544A 6648 AB544A-J20131 2
ClientB AB544A 6648 AB544A-J20151 3
ClientB AB544A 6648 AB544A-J2015T 4
ClientB BD944A 6679 BD944A-D20131 5
ClientC CA778A 12073 CA778A-J20151 6
我想要的输出如下所示
[{
"ClientName":"ClientA",
"DnoList":[
{
"DnoId":"16711",
"Dno":"AB342A",
"DfullList":[
{
"DfullId":"1",
"Dfull":"AB342A-J2015"
}
]
}
]
},
{
"ClientName":"ClientB"
"DnoList":[
{
"DnoId":"6648",
"Dno":"AB544A",
"DfullList":[
{
"DfullId":"2",
"Dfull":"AB544A-J20131"
},{
"DfullId":"3",
"Dfull":"AB544A-J20151"
},{
"DfullId":"4",
"Dfull":"AB544A-J2015T"
}
]
},
{
"DnoId":"6679",
"Dno":"BD944A",
"DfullList":[ {
"DfullId":"5",
"Dfull":"BD944A-D20131"
} ]
}
]
}]
现在我找到了两种方法来获得这种类型的输出 1.用嵌套数组创建三个不同的类,然后循环遍历 2. 循环遍历datatable/list ??
但我没有接近它。
谁能提供更好的方法。
编辑:我正在使用的 Linq 查询如下
var objlist = from tblA in context.TableA
join tblB in context.TableB on tblA.lng_clientid equals tblB.lng_id
where tblA.int_deleted.Equals(0)
select new Client()
{
ClientName = tblA.str_client,,
DnoId = tblA.lng_dnoid,
Dno = tblA.str_dno,
Dfull = tblA.str_dfull,
DfullId = tblA.lng_id
};
类如下
public class Client
{
public string ClientName { get; set; }
public int DnoId { get; set; }
public string Dno { get; set; }
public string Dfull { get; set; }
public int DfullId { get; set; }
}
我的其他方法是不同的类,如下所示
public class MyData
{
public string ClientName { get; set; }
public List<DnoList> DnoLists { get; set; }
}
public class DnoList
{
public int DnoId { get; set; }
public string Dno { get; set; }
public List<DfullList> DfullLists { get; set; }
}
public class DfullList
{
public int DfullId { get; set; }
public string Dfull { get; set; }
}
【问题讨论】:
-
你可以循环遍历表格,形成类似的数据结构并序列化。
-
@Mahajan344 你想要什么?为上述场景定义最佳类结构......或者如何使用一个类场景??
-
请更新您的 LINQ 查询以包含
DnoId