Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a two list of Client Class
C#
public class Client 
{
    public int ClientID { get; set; }
    public string ClientName { get; set; }
    public string companyName { get; set; }
    public string companyLocation { get; set; }
}


first list for NewClient
and second for oldClient

i need Three list
Newly Added clients
Deleted Clients
Common Clients

i tried following way but i want result on ClientID and Company Name
how i can add another condition so i can get difference based on these two values

I'm Getting Added and Deleted Values
but i'm not getting Common Clients

What I have tried:

i tried with
C#
List<Client> lstOld_Client = new List<Client>();
           List<Client> lstNew_Client = new List<Client>();


           Client C1 = new Client() { ClientName = "A", ClientID = 1, CompanyLocation = "Pune", CompanyName = "C1" };
           Client C2 = new Client() { ClientName = "A", ClientID = 1, CompanyLocation = "Pune", CompanyName = "C2" };

           lstNew_Client.Add(C1);
           lstNew_Client.Add(C2);

           lstOld_Client.Add(C1);

           var Added = lstNew_Client.Where(a => !lstOld_Client.Any(x => x.ClientID == a.ClientID && x.CompanyName == a.CompanyName)).ToList();
           var deleted = lstOld_Client.Where(a => !lstNew_Client.Any(x => x.ClientID == a.ClientID && x.CompanyName == a.CompanyName)).ToList();

           var commonElements = lstNew_Client.Where(a => lstOld_Client.Any(x => x.ClientID == a.ClientID && x.ClientName == a.ClientName)).ToList();

           var OldTemplete = lstOld_Client.Where(a => commonElements.Any(x => x.ClientID == a.ClientID && x.ClientName == a.ClientName)).FirstOrDefault();
           var NewTemplete = lstNew_Client.Where(a => commonElements.Any(x => x.ClientID == a.ClientID && x.ClientName == a.ClientName)).FirstOrDefault();
Posted
Updated 19-Aug-18 21:32pm
v5
Comments
George Jonsson 27-Apr-16 0:47am    
Shouldn't
commonElements = NewClient.Where(a => oldClient.Any(x => x.ClientID== a.ClientID)).ToList();
be
commonElements = CommonClient.Where(a => oldClient.Any(x => x.ClientID== a.ClientID)).ToList();

That said, try
commonElements = CommonClient.Where(a => oldClient.Any(x => x.ClientID == a.ClientID && x.companyName == a.companyName)).ToList();
(assuming your LINQ works ok otherwise)
kedar001 27-Apr-16 2:16am    
please check my Updated "What I have tried"

I'm getting Correct result for Added And Deleted ,but not able to get that common
i'm expecting "C1" in common

1 solution

C#
Common = NewClient.Where(n => oldClient.Any(o => o.ClientID == n.ClientID)).ToList();
        Deleted = oldClient.Where(o => !NewClient.Any(n => n.ClientID == o.ClientID)).ToList();
        NewlyAdded = NewClient.Where(n => !oldClient.Any(o => o.ClientID == n.ClientID)).ToList();
 
Share this answer
 
Comments
kedar001 27-Apr-16 1:37am    
please check my Updated "What I have tried"

I'm getting Correct result for Added And Deleted ,but not able to get that common
i'm expecting "C1" in common
Karthik_Mahalingam 27-Apr-16 1:49am    
getting 'A' twice
why you are checking with client name & company name..
ID is unique right?
kedar001 27-Apr-16 2:02am    
No,these are records for Company having different Client
Karthik_Mahalingam 27-Apr-16 2:17am    
ok fine. what you are expecting in commonElements list?
as per the query and logic it will return 2 items.
kedar001 27-Apr-16 2:21am    
I'm Expecting "C1" in common as we have added C1 in both list old and New

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900