mercredi 17 mars 2021

merge datatable as array of json with condition asp.net web api

i have two datatables DataTable dt1 and DataTable dt2 dt1 is filled with products and dt2 is filled with product flavors both tables have a common id that is "productcode"

with newtonsoft json serializer I'm getting the response as

{
  "products": [
    {
      "productcode": 1676,
      "type": "BEER BOTTLE-24*325ML",
      "image_url": "http://localhost/images/temp.png"
    },
    {
      "productcode": 1677,
      "type": "RED FIGHTER CAN-24*250ML",
      "image_url": "http://localhost/images/temp.png"
    },
    {
      "productcode": 1678,
      "type": "POWER ENERGY CAN 24*330ML",
      "image_url": "http://localhost/images/temp.png"
    }
  ]
}

and

{
  "flavor": [
    {
      "productcode": 1676,
      "ProductFlavorID": 9351,
      "Flavor": "APPLE"
    },
    {
      "productcode": 1676,
      "ProductFlavorID": 9352,
      "Flavor": "STRAWBERY"
    },
    {
      "productcode": 1673,
      "ProductFlavorID": 9353,
      "Flavor": "respery"
    },
    {
      "productcode": 1678,
      "ProductFlavorID": 9354,
      "Flavor": "RUMMAN"
    }
  ]
}

I need to join this jsons with condition where json1 productid= json2 productid and result like

{
  "products": [
    {
      "productcode": 1676,
      "type": "BEER BOTTLE-24*325ML",
      "image_url": "http://localhost/images/temp.png",
      "flavor": [
        {
          "productcode": 1676,
          "ProductFlavorID": 9351,
          "Flavor": "APPLE"
        },
        {
          "productcode": 1676,
          "ProductFlavorID": 9352,
          "Flavor": "STRAWBERY"
        }
      ]
    },
    {
      "productcode": 1677,
      "type": "RED FIGHTER CAN-24*250ML",
      "image_url": "http://localhost/images/temp.png",
      "flavor": [
        {
          "productcode": 1677,
          "ProductFlavorID": 9353,
          "Flavor": "respery"
        }
      ]
    },
    {
      "productcode": 1678,
      "type": "POWER ENERGY CAN 24*330ML",
      "image_url": "http://localhost/images/temp.png"
    }
  ]
}

how can I archive this...




Aucun commentaire:

Enregistrer un commentaire