如何在Json(嵌套)中包含派生表列

时间:2015-11-16 12:24:37

标签: json entity-framework asp.net-web-api

我在library(data.table) dt <- fread("listing_id date city type host_id availability 703451 25/03/2013 amsterdam Entire_home/apt 3542621 245 703451 20/04/2013 amsterdam Entire_home/apt 3542621 245 703451 28/05/2013 amsterdam Entire_home/apt 3542621 245 703451 15/07/2013 amsterdam Entire_home/apt 3542621 245 703451 30/07/2013 amsterdam Entire_home/apt 3542621 245 703451 19/08/2013 amsterdam Entire_home/apt 3542621 245") dt$date <- as.Date(dt$date, "%d/%m/%Y") dt[, .N, by=year(date)] # year N # 1: 2013 6 dt[, .N, by=.(year(date), month(date))] # year month N # 1: 2013 3 1 # 2: 2013 4 1 # 3: 2013 5 1 # 4: 2013 7 2 # 5: 2013 8 1 dt[, .N, by=date] # or: dt[, .N, by=.(year(date), month(date), day(date)] # date N # 1: 2013-03-25 1 # 2: 2013-04-20 1 # 3: 2013-05-28 1 # 4: 2013-07-15 1 # 5: 2013-07-30 1 # 6: 2013-08-19 1

中有两个Entities

enter image description here

VS 2015 Model(家长)有Halaqat_TestPrimary KeyMsqID(儿童)有Halaqat_Test2FOREIGN Key

这两个实体之间没有链接或任何关联。 昨天我将每个实体链接到另一个实体。使用此代码

MsqID

但问题是,子 var result = db.Halaqati_Test.GroupJoin (db.Halaqati_Test2, c => c.msq_id, s => s.MsqID, (c, s) => new { Mosque = c, Rings = s }); return result; Table(未包含在Halaqati_Test2中的父资产中

看起来像:

Json

但是应该和预期的json是:

[
  {
    "Mosque": {
      "msq_id": 3,
      "MsqName": "Center 1",
      "RowNum": 1
    },
    "Rings": [
      {
        "RingID": 613,
        "RingName": "aaaa",
        "MsqID": 3,
        "RowNum": 231
      },
      {
        "RingID": 614,
        "RingName": "bbb",
        "MsqID": 3,
        "RowNum": 232
      },
      {
        "RingID": 615,
        "RingName": "ccc",
        "MsqID": 3,
        "RowNum": 233
      }
    ]
  }
]

更新

enter image description here 我想将Halaqati_Test2中的派生实体视为(父)到view_stdwithrings(Child)

[
  {
    "Mosque": {
      "msq_id": 3,
      "MsqName": "Center 1",
      "RowNum": 1,
      "Rings": [
        {
          "RingID": 613,
          "RingName": "aaaa",
          "MsqID": 3,
          "RowNum": 231
        },
        {
          "RingID": 614,
          "RingName": "bbb",
          "MsqID": 3,
          "RowNum": 232
        },
        {
          "RingID": 615,
          "RingName": "ccc",
          "MsqID": 3,
          "RowNum": 233
        }
      ]
    }
  }
]

我怎么能在戒指下制作这个级别,所以戒指会有更多的学生 我该怎么做才能确保孩子进入父节点。

1 个答案:

答案 0 :(得分:1)

选择此行 - 从而创建生成的json结构。因为你在同一级别选择“清真寺”和“戒指”,你会得到一个反映它的json结构:

(c, s) => new { Mosque = c, Rings = s });

尝试将其更改为:

(c, s) => new { Mosque = new { msq_id = c.msq_id, MsqName = c.MsqName, RowNum = c.RowNum, Rings = s }});

(免责声明:我这里没有编码环境,所以我无法测试此代码)