Guest User

Guest User

  • Tech Writer
  • 5
  • 429

Building linq query to build json response object

Jul 2 2021 4:33 PM

I am having a hard time writing LINQ query for building  JSON response. Can some one help me with this?
Requirement: A movie can be produced by two productions and many assistant directors but movie will have a single director.
I am fetching this data from DB

PRODUCTIONID  MOVIENAME        DID     PLACE    DNAME    AID        ANAME

-----------------------------------------------------------------------------------------------------------------
1                                    DDLJ             11        DELHI      RAJ        111        Name 1
1                                    DDLJ             11        DELHI     RAJ         112        Name 2
1                                    DDLJ              11       DELHI      RAJ        113        Name 3
1                                    DDLJ             11        DELHI    RAJ           114        NAME 4

2                                    DDLJ             11        DELHI      RAJ        111        Name 1
2                                    DDLJ             11        DELHI     RAJ         112        Name 2
2                                    DDLJ              11       DELHI      RAJ        113        Name 3
2                                    DDLJ             11        DELHI    RAJ           114        NAME 4

2                                      DON2           22       NOIDA     SAM        221        Name 5
2                                       DON2          22       NOIDA     SAM        222        Name 6
2                                       DON2          22        NOIDA    SAM        223        Name 7
2                                        DON2        22         NOIDA    SAM        224        NAME 8

My JSON response should be like:

{
 
      "Movies": [
        {
          "ProductionID": "string",
          "MovieInfo": [
            {
              "MovieName": "string",
              "Directorinfo": [
                {
                  "Did": "string",
                  "Dname": "string",
                  "Place": "string"
                }
              ],
              "AssistantDirectors": [
                {
                  "Aid": "string",
                  "Aname": "string",
                }
              ]
            }
          ]
        }
      ]
    }
 
}


Sample output that is expected:


 

"content": [
    {
      "Movies": [
        {
          "ProductionID”: “1”,
          "MovieInfo": [
            {
              "MovieName": "DDLJ",
              "Directorinfo": [
                {
                  "Did": "11",
                  "Dname": "RAJ",
                  "Place": "delhi"
                }
              ],
              "AssistantDirectors": [
                {
                  "Aid": "111",
                  "Aname": "NAME 1"
                },
                {
                  "Aid": "112",
                  "Aname": "NAME 2"
                },
                {
                  "Aid": "113",
                  "Aname": "NAME 3"
                },
                {
                  "Aid": "114",
                  "Aname": "NAME 4"
                }
              ]
            }
          ]
        },
        {
          "ProductionID": "2",
          "MovieInfo": [
            {
              "MovieName": "DDLJ",
              "Directorinfo": [
                {
                  "Did": "11",
                  "Dname": "RAJ",
                  "Place": "delhi"
                }
              ],
              "AssistantDirectors": [
                {
                  "Aid": "111",
                  "Aname": "NAME 1"
                },
                {
                  "Aid": "112",
                  "Aname": "NAME 2"
                },
                {
                  "Aid": "113",
                  "Aname": "NAME 3"
                },
                {
                  "Aid": "114",
                  "Aname": "NAME 4"
                }
              ]
            },
            
            {
              "Moviename": "DON",
              "Directorinfo": [
                {
                  "Did": "22",
                  "DName": "SAM",
                  "Place": "NOIDA"
                }
              ],
              "AssistantDirectors ": [
                {
                  "AID": "221",
                  "aName": "NAME 5"
                },
                {
                  "AID": "222",
                  "aName": "NAME 6"
                },
                {
                  "AID": "223",
                  "aName": "NAME 7"
                },
                {
                  "AID": "224",
                  "aName": "NAME 8"
                },

              ]
            }
          ]
        }
      ]
    }
  ],
 
  }
}

 


Answers (1)