1

我想学习这样做,因为它让我头疼: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,但我无法获得所有内容的嵌套输出。我能做的最好的事情是获得一个嵌套的 Parent -> Students[] 输出,但无法弄清楚如何到达 LineItems 和 Products。

4

1 回答 1

0

以下解决方案可能对您有所帮助

我正在使用 Nortwnd 示例数据库,有 3 个表,Customers,Orders,OrdersDetails,如您所知,这些表之间的关系客户有很多订单有很多产品

以下是使用 System.Web.Script.Serialization 的工作示例;var json = 新列表();

       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
                    }
                ]
            }
        ]
    }
]
于 2012-12-21T13:23:48.577 回答