Click here to Skip to main content
15,918,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
List<ABC> lst = new List<ABC>()
{
    new ABC { ID = 1, birthdate = DateTime.Now, name = "Vijay", joinDate = DateTime.Now },
    new ABC { ID = 2, birthdate = DateTime.Now, name = "Sagar", joinDate = DateTime.Now },
    new ABC { ID = 3, birthdate = DateTime.Now, name = "Dhaval", joinDate = DateTime.Now },
};

List<string> availablefield = new List<string>();
availablefield.Add("ID");
availablefield.Add("birthdate");
availablefield.Add("name");


What I have tried:

how to compare two list and getting data from first list but only getting data from which pass to second list.

I belongs this type of output

ID = 1, birthdate = DateTime.Now, name = "Vijay"
ID = 2, birthdate = DateTime.Now, name = "Sagar"
Posted
Updated 1-Apr-16 4:27am
v2
Comments
Karthik_Mahalingam 1-Apr-16 10:17am    
what do you want to compare exactly
Giletwala Dhaval 1-Apr-16 10:19am    
Hi Kartik,

My final out is this type...

ID = 1, birthdate = DateTime.Now, name = "Vijay"
ID = 2, birthdate = DateTime.Now, name = "Sagar

if i am remove name in second list than name is remove.
Karthik_Mahalingam 1-Apr-16 10:21am    
are you storing names in the second list ?
Giletwala Dhaval 1-Apr-16 10:23am    
yes
Karthik_Mahalingam 1-Apr-16 10:31am    
checkout my solution.

1 solution

using LINQ (Language-Integrated Query)[^] you can do it easier
C#
List<string> availablefield = new List<string>();
       availablefield.Add("Vijay");
       availablefield.Add("Sagar");

       lst = lst.Where(k => availablefield.Contains(k.name)).ToList();



C#
// using foreach loop
           List<ABC> lstOutput = new List<ABC>();
           foreach (ABC item in lst)
               if (availablefield.Contains(item.name))
                   lstOutput.Add(item);
 
Share this answer
 
v3
Comments
Giletwala Dhaval 2-Apr-16 0:56am    
Thanks Kartik....
Karthik_Mahalingam 2-Apr-16 0:59am    
welcome :)

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