将标准 EF 列表转换为嵌套 JSON
Convert a standard EF list to a nested JSON
我的 ASPX 页面中有一个 WEB 方法,它使用 Entity Framework 从 SQL 数据库中检索列表。
Using rep As New RBZPOSEntities
q = rep.getSalesByLocation(fDate, tDate).ToList
End Using
此后我使用 javascriptserializer 将此列表转换为 JSON 字符串
Dim jss As JavaScriptSerializer = New JavaScriptSerializer()
Dim json As String = jss.Serialize(q)
所以上面的效果很好,我可以在客户端使用 AJAX 来成功显示结果。
我现在遇到的问题是将平面列表转换为嵌套 JSON 字符串。
因此,请考虑如下列表:
locationName as string
MonthName as string
totalAmount as string
需要像这样转换成JSON:
[{locationName:'Tokyo',totalAmount:[100,200,300,400]},
{locationName:'New York',totalAmount:[500,600,700,800]}]
因此,上述情况中的 totalAmount 值对应于特定月份的 Location 的 totalAmounts。例如。东京总金额一月份是100,二月份是200等等
我能做什么:
我可以创建一个嵌套列表并用 EF 的结果填充它,然后序列化为 JSON.
我在问什么:
还有其他更清洁的方法吗?
谢谢
如果您使用中间类型,它可能会有所帮助。类似于:
class RbzposInfo {
public string Location {get;set;}
public List<MonthlyRevenue> Monthlies {get;set;}
}
class MonthlyRevenue {
public string Month {get;set;}
public int Revenue {get;set;}
}
var result = new List<RbzposInfo>();
foreach (var r in q) { // q from your code
var info = new RbzposInfo();
// TODO: put r values into info.
result.Add(info);
}
Dim json As String = jss.Serialize(result);
(PS,对不起,我在你身上混用了 .NET 语言。我的代码是 C#。)
正如托德所说,您需要将平面列表转换为另一种中间类型的列表。让我们将您现有的类型称为 "InputType" 并将您想要的类型称为 "OutputType"
Public Class InputClass
Public locationName as String
Public MonthName as String
Public totalAmount as String
End Class
Public Class OutputClass
Public LocationName As String
Public TotalAmount As List(Of String)
Public Sub New(groupedInputClass As IGrouping(Of String, InputClass))
LocationName = groupedInputClass.Key
TotalAmount = groupedInputClass.Select(Function(x) x.totalAmount).ToList()
End Sub
End Class
然后您需要做的就是将 InputType 列表转换为 OutputType 列表,这使用 LINQ 非常简单。
Public Class MappingHelper
Public Function Map (inputList as List(Of InputClass)) As List(Of OutputClass)
Dim groupedByLocation = inputList.GroupBy(Function(k) k.LocationName)
Dim nestedLocations = groupedByLocation.Select(Function(x) new OutputClass(x)).ToList()
Return nestedLocations
End Function
End Class
我的 ASPX 页面中有一个 WEB 方法,它使用 Entity Framework 从 SQL 数据库中检索列表。
Using rep As New RBZPOSEntities
q = rep.getSalesByLocation(fDate, tDate).ToList
End Using
此后我使用 javascriptserializer 将此列表转换为 JSON 字符串
Dim jss As JavaScriptSerializer = New JavaScriptSerializer()
Dim json As String = jss.Serialize(q)
所以上面的效果很好,我可以在客户端使用 AJAX 来成功显示结果。
我现在遇到的问题是将平面列表转换为嵌套 JSON 字符串。 因此,请考虑如下列表:
locationName as string
MonthName as string
totalAmount as string
需要像这样转换成JSON:
[{locationName:'Tokyo',totalAmount:[100,200,300,400]},
{locationName:'New York',totalAmount:[500,600,700,800]}]
因此,上述情况中的 totalAmount 值对应于特定月份的 Location 的 totalAmounts。例如。东京总金额一月份是100,二月份是200等等
我能做什么: 我可以创建一个嵌套列表并用 EF 的结果填充它,然后序列化为 JSON.
我在问什么: 还有其他更清洁的方法吗?
谢谢
如果您使用中间类型,它可能会有所帮助。类似于:
class RbzposInfo {
public string Location {get;set;}
public List<MonthlyRevenue> Monthlies {get;set;}
}
class MonthlyRevenue {
public string Month {get;set;}
public int Revenue {get;set;}
}
var result = new List<RbzposInfo>();
foreach (var r in q) { // q from your code
var info = new RbzposInfo();
// TODO: put r values into info.
result.Add(info);
}
Dim json As String = jss.Serialize(result);
(PS,对不起,我在你身上混用了 .NET 语言。我的代码是 C#。)
正如托德所说,您需要将平面列表转换为另一种中间类型的列表。让我们将您现有的类型称为 "InputType" 并将您想要的类型称为 "OutputType"
Public Class InputClass
Public locationName as String
Public MonthName as String
Public totalAmount as String
End Class
Public Class OutputClass
Public LocationName As String
Public TotalAmount As List(Of String)
Public Sub New(groupedInputClass As IGrouping(Of String, InputClass))
LocationName = groupedInputClass.Key
TotalAmount = groupedInputClass.Select(Function(x) x.totalAmount).ToList()
End Sub
End Class
然后您需要做的就是将 InputType 列表转换为 OutputType 列表,这使用 LINQ 非常简单。
Public Class MappingHelper
Public Function Map (inputList as List(Of InputClass)) As List(Of OutputClass)
Dim groupedByLocation = inputList.GroupBy(Function(k) k.LocationName)
Dim nestedLocations = groupedByLocation.Select(Function(x) new OutputClass(x)).ToList()
Return nestedLocations
End Function
End Class