Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# list.except compare classes with IEqualityComparer

// Except gives you the items in the first set but not the second
        var equalityComparer = new MyClassEqualityComparer();
        var InList1ButNotList2 = List1.Except(List2, equalityComparer);
        var InList2ButNotList1 = List2.Except(List1, equalityComparer);
// Intersect gives you the items that are common to both lists    
        var InBothLists = List1.Intersect(List2);

public class MyClass
{
    public int i;
    public int j;
}

class MyClassEqualityComparer : IEqualityComparer<MyClass>
{
    public bool Equals(MyClass x, MyClass y)
    {
        return x.i == y.i &&
               x.j == y.j;
    }

    public int GetHashCode(MyClass obj)
    {
        unchecked
        {
            if (obj == null)
                return 0;
            int hashCode = obj.i.GetHashCode();
            hashCode = (hashCode * 397) ^ obj.i.GetHashCode();
            return hashCode;
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: string to float c# 
Csharp :: How to get the value of an input button in an ASP.NET Core MVC controller 
Csharp :: variable with letters and numbers in C# 
Csharp :: credit card validation in c# 
Csharp :: c# get Full Exception message if InnerException is not NULL 
Csharp :: c# 2 timespan return yesterday 
Csharp :: linq query languages 
Csharp :: C# Func Delegate 
Csharp :: move position smoth unity 
Csharp :: Accepts one of 1, 2, x or X, or nothing 
Csharp :: netmath 
Csharp :: lsbCat.Items.Clear();lsbCat.Items.AddRange(Cats.ToArray());txtCat.Clear(); 
Csharp :: sliding window algorithm in c# 
Csharp :: Make child unaffected by parents rotation Unity 
Csharp :: c# get app FileVersion 
Csharp :: two question marks c# 
Csharp :: visual studio private field underscore 
Csharp :: Set database timeout in Entity Framework 
Csharp :: get web api relative path 
Csharp :: asp.net razor get list without refresh 
Csharp :: C# Payroll 
Csharp :: replace update claims c# 
Csharp :: distinct and not null c# 
Csharp :: how to mock abstract httpcontext using moq .net core 
Csharp :: short in c# 
Csharp :: how to write an if statement with two checkboxes in c# 
Csharp :: remove lines from textfile 
Csharp :: net use error 67 
Csharp :: C# Read Excel columns header return to list 
Csharp :: ef null check 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =