Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

c# – update list of objects from another list

I have a list of object.

"result": [
    {
        "primaryKey": "123",
        "count": 0,
        "details": [
            {
                "employeeName": "Chris",
            }
        ],
    },
    {
        "primaryKey": "456",
        "count": 10,
        "details": [
            {
                "employeeName": "Nick",
            }
        ],
    },
]

And another list.

"result": [
    {
        "foreignKey": "123",
        "details": [
            {
                "employeeName": "Sean",
            }
        ],
    },
    {
        "foreignKey": "789",
        "details": [
            {
                "employeeName": "Andrew",
            }
        ],
    },
]

I want to update the details property of the first list with the contents of second list details, if the primary key and foreign key matches.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

foreach(obj item in result1)
{
    if(result2.where(x => foreignKey == item.primaryKey).Any())
    {
       string EmpName = result2.where(x => foreignKey == item.primaryKey).FirstOrDefault().employeeName;
       item.employeeName = EmpName ;
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading