lundi 20 août 2018

how to get the updated or modified list record by comparing 2 list using linq or reflection

List<NeWClient> ListofNewClient = new List < NeWClient >{
                new NeWClient {   ClientID = 1, ClientName = "Nilesh", companyLocation = "Mumbai", companyName = "CG" },
                 new NeWClient {   ClientID = 101, ClientName = "Rahul", companyLocation = "USA", companyName = "TATA" }
               };

        List<OldClient> ListofOldClient = new List<OldClient>{
            new OldClient {   ClientID = 1, ClientName = "Nilesh", companyLocation = "Mumbai", companyName = "CG" },
            new OldClient {   ClientID = 3, ClientName = "Mary", companyLocation = "UK", companyName = "KG" },
            new OldClient {   ClientID = 7, ClientName = "jhon", companyLocation = "USA", companyName = "TG" },
            new OldClient {   ClientID = 9, ClientName = "Tom", companyLocation = "India", companyName = "PG" }
           };

    var  Common = ListofNewClient.Where(n => ListofOldClient.Any(o => o.ClientID == n.ClientID)).ToList();
    var  Deleted = ListofOldClient.Where(o => !ListofNewClient.Any(n => n.ClientID == o.ClientID)).ToList();
    var  NewlyAdded = ListofNewClient.Where(n => !ListofOldClient.Any(o => o.ClientID == n.ClientID)).ToList();

Above works fine for common , deleted, NewlyAdded but i am not able to find the updated or modified the newClinet by comparing with oldClient

        List<NeWClient> ListofNewClient1 = new List<NeWClient>{
            new NeWClient {   ClientID = 200, ClientName = "Dinesh", companyLocation = "Surat", companyName = "nts" },
           };

        List<OldClient> ListofOldClient1 = new List<OldClient>{
            new OldClient {   ClientID = 200, ClientName = "Dinesh", companyLocation = "Mumbai", companyName = "nts" },
            new OldClient {   ClientID = 3, ClientName = "Mary", companyLocation = "UK", companyName = "KG" },
           };

Required Result : ListofNewClient1 values are changed here when we compare with ListofOldClient1 so required result will be from ListofNewClient1 which is given below

ClientID = 200, ClientName = "Dinesh", companyLocation = "Surat", companyName = "nts"





Aucun commentaire:

Enregistrer un commentaire