Click here to Skip to main content
16,016,088 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am having 2 generic list i just need to compare both list and reomve duplicate from that and save that unduplicate value in third list

What I have tried:

I have tried linq query but it is giving me duplicate from both the list but i need which is not duplicate
Posted
Updated 4-Mar-16 4:18am
Comments
[no name] 4-Mar-16 6:01am    
Can you provide a demo what do you want exactly in third list..
Philippe Mori 4-Mar-16 8:29am    
And also where is your code (query)? Do you really that us can read in you mind or do you expect us to access your computer by the web...

C#
foreach (var s in lst1)
{
    lst3.Add(s);
}

foreach (var s2 in lst2)
{
    bool existed = false;

    foreach (var s3 in lst3)
    {
        if (s2 == s3)
        {
            existed = true;
            break;
        }
    }

    if (!existed)
    {
        lst3.Add(s2);
    }
}
 
Share this answer
 
If I'm reading that right, you want the disjunction of the two lists - all of the items which are in one of the lists, but not in both.

There isn't a built-in LINQ method to do that, but it's fairly simple to implement:
C#
var disjunction = firstList.Except(secondList) // Items in the first list but not in the second
    .Concat(secondList.Except(firstList))      // Items in the second list but not in the first
    .ToList();

// Or:
var disjunction = new HashSet<YourType>(firstList);
disjunction.SymmetricExceptWith(secondList);

If your type doesn't implement IEquatable<T>[^], then you'll need to create a class which implements IEqualityComparer<T>[^], and pass an instance of that class as the second parameter to the Except method or the HashSet<T> constructor.
 
Share this answer
 
Comments
Sascha Lefèvre 4-Mar-16 6:20am    
+5

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