简体   繁体   English

将数据表合并为 json 的数组,条件为 asp.net web Z8A5DA52ED126447D359E70C0572

[英]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"我有两个数据表 DataTable dt1 和 DataTable dt2 dt1 填充了产品,dt2 填充了产品风味 两个表都有一个共同的 id,即“productcode”

with newtonsoft json serializer I'm getting the response as使用 newtonsoft json 序列化程序我得到的响应为

{
  "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我需要在 json1 productid= json2 productid 和结果如下的条件下加入这个 jsons

{
  "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...我怎样才能存档这个...

You can apporach this situation with linq , when iterating every product you can set flavors from dt2 by filtering with Where method.您可以使用linq来处理这种情况,在迭代每个产品时,您可以通过使用 Where 方法过滤从 dt2 设置风味。

Simple example would be like this:简单的例子是这样的:

using System.Linq;

products.ForEach(p => {
    p.flavors = flavors.Where(f => f.productcode == product.productcode).ToList();
});

Where method 哪里方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM