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...
You can apporach this situation with linq , when iterating every product you can set flavors from dt2 by filtering with Where method.
Simple example would be like this:
using System.Linq;
products.ForEach(p => {
p.flavors = flavors.Where(f => f.productcode == product.productcode).ToList();
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.