如何将列表放在C#中的json中?

时间:2018-01-07 05:43:50

标签: c# .net json linq

我在C#中有这个代码(只是一个例子):

var list1 = ( from x
    join y
    select new
    {
        id, name
    });

var counta = list1.count;
if (counta > 0)
{
    for (var i = 0; i < counta; i++)
    {
        var userid = list1[i].id;
        var user = user.Where(a => a.id == user).Select(a => a.userid).FirstOrDefault();
        var list2 = (
            from f
            join g.Where(a => a.userid = user)select new
            {
            hourId, hourName
            }
        );
        if (list2 > 0)
        {
            foreach (var product in list2)
            {
                var list3 = (
                    from p.
                    where (a => a.id == user)join q.
                    where (a => a.id == user)select new
                    {
                    productId, productName
                    });
            }
        }
    }
}

这里是list1,list2,list3关系:lis​​t1有很多list2,list2有很多list3,这里是我期望返回的JSON:

[
  {
    "list1": {
      "id": 1,
      "name": "Adam",
      "list2": [
        {
          "hourId": 1,
          "hourName": "08:00",
          "list3": [
            {
              "productId": 1,
              "productName": "Candy"
            },
            {
              "productId": 2,
              "productName": "Cookie"
            }
          ]
        },

        {
          "hourId": 2,
          "hourName": "09:00",
          "list3": [
            {
              "productId": 1,
              "productName": "Candy"
            },
            {
              "productId": 2,
              "productName": "Cookie"
            }
          ]
        }
      ]
    }
  }
]

所以我的问题是如何将list1,list2,list3放在一起以返回以下JSON?我是否必须再次加入LINQ中的3个列表?

谢谢!

0 个答案:

没有答案