I currently have a C# class that appears as follows:
namespace DBModel
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
public partial class ConnSrv
{
public int ID { get; set; }
[Required]
[StringLength(14)]
public string Service { get; set; }
[Required]
[StringLength(14)]
public string ConnectsToService { get; set; }
public virtual Service Service1 { get; set; }
public virtual Service Service2 { get; set; }
}
}
In my main program, I'm creating hashsets to work with these objects. The problem I'm encountering is that I would like to be able to use operators such as Contains, Except, ExceptWith, UnionWith, and so on on these hashsets.
For an object of the ConnSrv to be considered "equal", the Service and ConnectsToService values have to be equal. As such, I realize that in some way, I will have to override the Equals and GetHashCode operators for the class. I'm just not sure how to do so, whether it's through a straight override of the object class, by implementing IEquatable or IEquatable, or through some other method. Below are a couple simple examples of what I would expect the end result to be once implemented. If I'm way out in left field, please let me know. Thanks in advance for your time.
var testHS1 = new HashSet<ConnectingService>();
testHS1.Add(test1);
testHS1.Contains(test2); // Returns true
var testHS2 = new HashSet<ConnectingService>();
testHS2.Add(test1);
testHS2.Add(test2);
testHS2.Add(test3);
testHS2.Except(testHS1); // Expect end result to only contain test3