我有这个代码:
public class Customer
{
Int32 id { get; set; } = 0;
User user1 { get; set; } = null;
User user2 { get; set; } = null;
}
/* ... */
using (MySqlConnection conn = new MySqlConnection(Costanti.connessione))
{
conn.Open();
MySqlCommand m = new MySqlCommand("
SELECT c1.*, u1.*, u2.*
FROM customers as c1
inner join utenti u1 on u1.customer_id = c1.id
inner join utenti u2 on u2.customer_id = c1.id
", conn);
MySqlDataReader x = m.ExecuteReader();
DataTable dataTable = new DataTable();
dataTable.Load(x);
String json_string = Newtonsoft.Json.JsonConvert.SerializeObject(dataTable);
List<Customer> lista = new List<Customer>();
Newtonsoft.Json.JsonConvert.PopulateObject(json_string, lista);
conn.Close();
}
我如何将c1.*
select 的字段映射到 generic Customer customer_1
,然后再u1.*
映射u2.*
到customer_1
的属性?Newtonsoft.Json.JsonConvert.PopulateObject
不让我这样做。
中间json_string
看起来像:
[
{
"id":1,
"id_user":8,
"name":"manuel",
"id_user1":2,
"name1":"michael"
},
{
"id":2,
"id_user":3,
"name":"friedrich",
"id_user1":6,
"name1":"antony"
}
]
结果必须是由以下内容组成的列表:
Customer(with id=1)
,User1(8,"manuel")
和User2(2,"michael")
;Customer(with id=2)
,User1(3,"friedrich")
和User2(6,"antony")
.