【发布时间】:2020-08-27 12:25:01
【问题描述】:
我只是在寻找一些指导,让我走上正确的道路。我有 2 个 SQL 查询,一个返回客户列表,一个返回客户已下订单的列表。在 c# 中工作,我如何能够填充一个可以序列化为 json 的类。
我尝试了多种不同的方式,现在这是我班级最接近的方式......
public class jsonOrder
{
public string order_no { get; set; }
public string customer { get; set; }
public List<orderitem> items { get; set; }
public decimal grandtotal { get; set; }
public jsonOrder()
{
this.items = new List<orderitem>();
}
}
public class orderitem
{
public string itemcode{ get; set; }
public int quantity { get; set; }
public decimal amount { get; set; }
}
使用这个我可以让我的 json 看起来像这样......
[
{
"order_no": "12345",
"customer": "12",
"items": [],
"grand_total": 6.0000,
}
{
...another order...
}
]
如何让物品按顺序列出时间?
例如
{
"order_no": "12345",
"customer": "12",
"items": [
{"itemcode":"a","quantity":1,"amount":12.34}
{"itemcode":"b","quantity":2,"amount":6.12}
],
"grand_total": 24.5800
}
目前我的代码是
List<readOrder> orderhistory = new List<Models.readOrder>(ordHead.Rows.Count);
if (ordHead.Rows.Count > 0)
{
foreach (DataRow orders in ordHead.Rows)
{
orderhistory.Add(new readOrder(orders));
}
}
但这只是带回标题详细信息。
我目前从以下获取我的 SQL,但我对此很灵活......
_con = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
// Get all orders
DataTable ordHead = new DataTable();
var queryHead = "Select * from ORDERHEADER where customer = " + customer;
_Header_adapt = new SqlDataAdapter
{
SelectCommand = new SqlCommand(queryHead,_con)
};
_Header_adapt.Fill(ordHead);
//Get items within orders
DataTable ordDetail = new DataTable();
var queryDetail = "Select * from ORDERHISTORY where customer = " + customer;
_adapt = new SqlDataAdapter
{
SelectCommand = new SqlCommand(queryDetail, _con)
};
_adapt.Fill(ordDetail);
```
【问题讨论】:
-
如果我们的帖子对您有帮助,那么您至少需要给我们一票。