我试图通过列表中的JSON形式从web方法制作我的回复
First List属性
Public Class DataRes
Public Property RecID As Integer
Public Property filenum As Integer
Public Property name As String
Public Property msgcode As String
Public Property msgtitle As String
Public Property msg As String
Public Property PartnerData As List(Of PartnerData)
End Class
我的第二个列表属性
Public Class PartnerData
Public Property RecID As Integer
Public Property PrtStatus As Integer
Public Property ptfilenum As Integer
Public Property prfilenum As Integer
End Class
我分别按以下代码填写这些列表
Dim Ptdata As New List(Of DataRes)()
Using sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
Ptdata.Add(New DataRes() With { _
.RecID = sdr("RecID").ToString(), _
.filenum = sdr("filenum").ToString(), _
.name = sdr("name").ToString() _
})
End While
End Using
return JsonConvert.SerializeObject(Ptdata)
Dim Prtdata As New List(Of PartnerData)()
Using sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
Prtdata.Add(New PartnerData() With { _
.prfilenum = sdr("PrtFilenum").ToString(), _
.PrtStatus = sdr("PrtStatus").ToString(), _
.ptfilenum = sdr("PtFilenum").ToString(), _
.RecID = sdr("RecID").ToString() _
})
End While
End Using
return JsonConvert.SerializeObject(PartnerData)

我测试了两个列表,我从每个
获得了预期的结果我正在尝试将它们合并,我尝试了以下内容,
无法转换#system; system.collections.generic.list`1
类型的对象
表达式不会产生值。
我的目标JSON就是这样的
{
"PtRegData": {
"RecID": "1277",
"filenum": "15090248",
"name": "Ahmad",
"PartnerData": [{
"RecID": 2383,
"PrtStatus": 1
}, {
"RecID": 3387,
"PrtStatus": 0
}, {
"RecID": 3388,
"PrtStatus": 0
}]
}}
另外,在序列化之前,如何在列表中添加名称?
修改
Using sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
Ptdata.Add(New DataRes() With { _
.RecID = sdr("RecID").ToString(), _
.filenum = sdr("filenum").ToString(), _
.name = sdr("name").ToString(), _
.msgcode = sdr("msgcode").ToString(), _
.msgtitle = sdr("msgtitle").ToString(), _
.msg = sdr("msg").ToString() _
})
End While
End Using
Using sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
Prtdata.Add(New PartnerData() With { _
.prfilenum = sdr("PrtFilenum").ToString(), _
.PrtStatus = sdr("PrtStatus").ToString(), _
.ptfilenum = sdr("PtFilenum").ToString(), _
.RecID = sdr("RecID").ToString() _
})
End While
For Each p In Ptdata
p.PartnerData.AddRange(Prtdata.Where(Function(c) c.ptfilenum = p.filenum))
Next
End Using

答案 0 :(得分:1)
我们假设你有这些类型:
Public Class Parent
Public Property ParentId As Integer
Public Property ParentName As String
Public ReadOnly Property Children As New List(Of Child)
End Class
Public Class Child
Public Property ChildId As Integer
Public Property ParentId As Integer
Public Property ChildName As String
End Class
名称和属性并不重要。只有父/子关系很重要。父类型具有属性,该属性是子类型的集合。假设您有两个父项和子项列表:
Dim parents As New List(Of Parent)
Dim children As New List(Of Child)
您可以填充每个Children
对象的Parent
属性,如下所示:
For Each p In parents
p.Children.AddRange(children.Where(Function(c) c.ParentId = p.ParentId))
Next
如果您更喜欢长手版:
For Each p In parents
For Each c In children
If c.ParentId = p.ParentId Then
p.Children.Add(c)
End If
Next
Next
编辑:旧版本的VB(早于2017年)可能不支持上面显示的只读属性语法。以下代码适用于所有版本:
Public Class Parent
Public _children As New List(Of Child)
Public Property ParentId As Integer
Public Property ParentName As String
Public ReadOnly Property Children As List(Of Child)
Get
Return _children
End Get
End Property
End Class