Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
2.65/5 (3 votes)
See more:
Hi,
How to compare 2 different arraylist in vb.net/c#?

Example:
List1:                               List2:
SNo.   Address                       SNo.           Address 
1       AAAAA                         1              BBBBB
2       BBBBB                         2              CCCCC   
3       CCCCC                         3              DDDDD  

Expected Results:
SNo     Address
1.      AAAAA -----> This row is define Red color,(Because not in List2)

2.      BBBBB -----> This row is define Green color(Because it contains both Lists)

3.      CCCCC -----> This row is define Green color(Because it contains both Lists)

4.      DDDDD -----> This row is define Blue color(Because not in List1)


Thanks in Advance.
Posted
Comments
Vedat Ozan Oner 5-May-14 9:28am    
have you done anything to share with us?
[no name] 5-May-14 9:44am    
Should be fairly easy to do with LINQ. What have you tried?
CodeChecker 5689 5-May-14 9:45am    
List<int> abc = new List<int>();
List<int> abcd = new List<int>();

abc.Union(abcd);
CodeChecker 5689 5-May-14 9:50am    
List<int> abc = new List<int>();
List<int> abcd = new List<int>();

abc.Union(abcd);
//OR

foreach(var ele in abc)
{
if(abcd.contains(ele))
{
// Print the Result;
}
}
Sergey Alexandrovich Kryukov 5-May-14 10:39am    
To start with, never use ArrayList. Use System.Collections.Generic.List instead.
As to your comparison... what have you tried? Everything above is just not serious...
—SA

1 solution

C#
var onlyinfirst = from s in list1 where !list2.Contains(s) select s;
var onlyinsecond = from s in list2 where !list1.Contains(s) select s;
var onboth = from s in list1 where list2.Contains(s) select s;

C#

 
Share this answer
 
Comments
Mohankumar.Engain 6-May-14 0:55am    
Thanks #Ravi.
Vi(ky 6-May-14 3:11am    
You're 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