我想学会这样做,因为它让我很头疼:S
目标:
使用尽可能少的数据库查询来获取我的应用程序的逻辑JSON对象。
方案
学校的应用程序,父母可以为孩子的活动付费。 父母有很多学生,学生有很多项目(订单),一个项目作为学生和产品之间的联接表。
型号:
public class Parent
{
public int ParentId { get; set; }
public string FullName { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
public class Student
{
public int StudentId { get; set; }
public string FullName { get; set; }
public int ParentId { get; set; }
public virtual Parent Parent { get; set; }
public virtual ICollection<LineItem> LineItems { get; set; }
}
public class Product
{
public int ProductId { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public virtual ICollection<LineItem> LineItems { get; set; }
}
public class LineItem
{
public int Id { get; set; }
public int ProductId { get; set; }
public int StudentId { get; set; }
public int Discount {get; set;}
// ...
public virtual Product Product { get; set; }
public virtual Student Student { get; set; }
}
JSON我想生成:
{
"Parent": {
"ParentId": 10,
"FullName": "John Doe",
"Students": [
{
"StudentId": 12,
"FullName": "William Doe",
"ParentId": 10,
"LineItems": [
{
"Discount": 10,
"Price": 150,
"Name": "Student trip to Washington"
},
{
"Discount": 10,
"Price": 20,
"Name": "Halloween party"
}
]
},
{
"StudentId": 15,
"FullName": "Kate Mary-Jane Doe",
"ParentId": 10,
"LineItems": [
{
"Discount": 10,
"Price": 110,
"Name": "Spring Break to Mexico"
}
]
}
]
}
}
备注:
我已尝试使用View Models和AutoMapper,但我无法获得所有内容的嵌套输出。我能做的最好的是获得一个嵌套的父 - &gt;学生[]输出,但无法弄清楚如何到达LineItems和产品。
答案 0 :(得分:0)
以下解决方案可能对您有所帮助
我正在使用Nortwnd Sample数据库,包含3个表,Customers,Orders,OrdersDetails,如您所知,这些表之间的关系客户有很多订单有很多产品
以下是工作示例 使用System.Web.Script.Serialization; var json = new List();
foreach (var c in customers)
{
json.Add(new
{
c.CustomerID,
custname =c.CompanyName,
Orders = c.Orders.Select(o => new
{
o.OrderID,
o.OrderDate,
OrderDetails=o.Order_Details.Select(d=> new{
d.OrderID,
d.ProductID
})
})
});
}
var serializer = new JavaScriptSerializer();
var jsonString = serializer.Serialize(json);
Response.Write(jsonString.ToString());
输出
[
{
"CustomerID": "WOLZA",
"custname": "Wolski Zajazd",
"Orders": [
{
"OrderID": 10374,
"OrderDate": "/Date(849724200000)/",
"OrderDetails": [
{
"OrderID": 10374,
"ProductID": 31
},
{
"OrderID": 10374,
"ProductID": 58
}
]
},
{
"OrderID": 10611,
"OrderDate": "/Date(869769000000)/",
"OrderDetails": [
{
"OrderID": 10611,
"ProductID": 1
},
{
"OrderID": 10611,
"ProductID": 2
},
{
"OrderID": 10611,
"ProductID": 60
}
]
},
{
"OrderID": 10792,
"OrderDate": "/Date(882815400000)/",
"OrderDetails": [
{
"OrderID": 10792,
"ProductID": 2
},
{
"OrderID": 10792,
"ProductID": 54
},
{
"OrderID": 10792,
"ProductID": 68
}
]
},
{
"OrderID": 10870,
"OrderDate": "/Date(886530600000)/",
"OrderDetails": [
{
"OrderID": 10870,
"ProductID": 35
},
{
"OrderID": 10870,
"ProductID": 51
}
]
},
{
"OrderID": 10906,
"OrderDate": "/Date(888345000000)/",
"OrderDetails": [
{
"OrderID": 10906,
"ProductID": 61
}
]
},
{
"OrderID": 10998,
"OrderDate": "/Date(891541800000)/",
"OrderDetails": [
{
"OrderID": 10998,
"ProductID": 24
},
{
"OrderID": 10998,
"ProductID": 61
},
{
"OrderID": 10998,
"ProductID": 74
},
{
"OrderID": 10998,
"ProductID": 75
}
]
},
{
"OrderID": 11044,
"OrderDate": "/Date(893269800000)/",
"OrderDetails": [
{
"OrderID": 11044,
"ProductID": 62
}
]
}
]
}
]