How To Include the Derived Table Columns inside the Json (Nested)

旧巷老猫 提交于 2019-12-25 07:39:34

问题


I've two Entities In VS 2015 Model

Halaqat_Test (Parent) has Primary Key (MsqID) Halaqat_Test2 (Child) has FOREIGN Key (MsqID)

there is no link or Any Association between those two entities. yesterday i do link each entity to another. by using this Code

    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;

but the problem is that the child Table columns Halaqati_Test2 (Not Included inside the parent Property In The Json)

it looks Like :

[
  {
    "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
      }
    ]
  }
]

but is should and the expected json is :

[
  {
    "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
        }
      ]
    }
  }
]

Update

I want to like a derived entity from the Halaqati_Test2 as (parent) to the view_stdwithrings (Child)

Halaqati_Test2.RingID = view_stdwithrings.rg_id

how i can make that level under of rings, so will be rings have more thatn one student what should i do exactly to make sure that the child comes inside the parent node.


回答1:


It is this line that selects - thus creates resulting json structure. Because you select "Mosque" and "Rings" on the same level, you get a json structure that reflects it:

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

Try changing it to:

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

(Disclaimer: i dont have a coding environment here, so i cant test this code)



来源:https://stackoverflow.com/questions/33735336/how-to-include-the-derived-table-columns-inside-the-json-nested

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!