I have used GroupBy and have got the value as below into individual array but the array consist of 5 sets of same Id so how do group them futhure to get something like expected result below , any idea would be helpful

[{
            "id": 116,
            "amgId": 168755,
            "amgName": "FIT",
            "isRefDemand": true,
            "forecastType": "N-2",
            "prodQty": 4801,
            "prodMonth": "2020/03",

        },
        {
            "id": 117,
            "amgId": 168755,
            "amgName": "FIT",
            "isRefDemand": true,
            "forecastType": "N-3",
            "prodQty": 3746,
            "prodMonth": "2020/04",

        }],
        [
        {
            "id": 61,
            "amgId": 168383,
            "amgName": "CIVIC",
            "isRefDemand": false,
            "forecastType": "N-2",
            "prodQty": 0,
            "prodMonth": "2020/03"

        },
        {
            "id": 62,
            "amgId": 168383,
            "amgName": "CIVIC",
            "isRefDemand": false,
            "forecastType": "N-3",
            "prodQty": 0,
            "prodMonth": "2020/04"

        }],
expected group like below

[
"amgId": 168755,
 "amgName": "FIT",
 "prodQty"{
 "prodQty1": 4801,
 "prodQty2": 3746,
 }
],
[
"amgId": 168383,
 "amgName": "CIVIC",
 "prodQty"{
 "prodQty1": 0,
 "prodQty2": 0,
 }
]

Recommended Answers

All 2 Replies

If you do a GroupBy on the properties amgId and amgName then you can fill the prodQty array.

If you can provide a small working example I can work with, then I can be more specific.

Here's something I put together. Not sure how it matches your situation. If you have additional questions/problems, please ask.

void Main()
{
    string json = "[{" +
            "\"id\": 116," +
            "\"amgId\": 168755," +
            "\"amgName\": \"FIT\"," +
            "\"isRefDemand\": true," +
            "\"forecastType\": \"N-2\"," +
            "\"prodQty\": 4801," +
            "\"prodMonth\": \"2020/03\"" +
        "}," +
        "{" +
            "\"id\": 117," +
            "\"amgId\": 168755," +
            "\"amgName\": \"FIT\"," +
            "\"isRefDemand\": true," +
            "\"forecastType\": \"N-3\"," +
            "\"prodQty\": 3746," +
            "\"prodMonth\": \"2020/04\"" +
        "}," +
        "{" +
            "\"id\": 61," +
            "\"amgId\": 168383," +
            "\"amgName\": \"CIVIC\"," +
            "\"isRefDemand\": false," +
            "\"forecastType\": \"N-2\"," +
            "\"prodQty\": 0," +
            "\"prodMonth\": \"2020/03\"" +
        "}," +
        "{" +
            "\"id\": 62," +
            "\"amgId\": 168383," +
            "\"amgName\": \"CIVIC\"," +
            "\"isRefDemand\": false," +
            "\"forecastType\": \"N-3\"," +
            "\"prodQty\": 0," +
            "\"prodMonth\": \"2020/04\"" +
        "}]";

    List<temp> list = JsonConvert.DeserializeObject<List<temp>>(json);
    var t = list.GroupBy(item => new { item.amgId, item.amgName }).Select(grp => new tempEx() { amgId = grp.Key.amgId, amgName = grp.Key.amgName, prodQty = grp.Select(q => q.prodQty).ToList() });

    string output = JsonConvert.SerializeObject(t);

}

class temp
{
    public int amgId;
    public string amgName;
    public int prodQty;
}

class tempEx
{
    public int amgId;
    public string amgName;
    public List<int> prodQty;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.