iequalitycomparer

C# 3.0: Need to return duplicates from a List<>

I have a List<> of objects in C# and I need a way to return those objects that are considered duplicates within the list. I do not need the Distinct resultset, I need a list of those items that I will be deleting from my repository. For the sake of this example, lets say I have a list of "Car" types and I need to know which of these ca...

How do you implement a IEqualityComparer<T> in VB.NET?

I have the following function that loops through a directory and checks for a specified folder and file: Private Function VerifyPath(ByVal root As String, ByVal folder As String, _ ByVal file As String) As Boolean Dim folders As New List(Of String), files As New List(Of String) Dim oDir As New IO.Di...

IEqualityComparer.Equals when used with IEnumerable.Contains is x or y the value in the list?

IEnumberable has an extension method Contains<T> which takes two parameters. The first parameter is the value to check for and the second is an implementation of IEqualityComparer. Looking at IEqualityComparer.Equals it takes two parameters named x and y, for the first and second objects to compare. My question is X or Y the value from...

HashSet constructor with custom IEqualityCompare defined by lambda?

Currently the HashSet<T> constructor that allows you to define your equality comparison yourself is the HashSet<T>(IEqualityComparer<T> comparer) constructor. I would like to define this EqualityComparer as a lambda. I found this blog post that has made a class that allows you to generate your comparer through lambda and then hides th...

Custom Generic.IEqualityComparer(Of T) - Compiler Errors

I am trying to implement a simple IEqulityComparer to use with LINQ collections. I have written the following code which is reduced to its simplest form for discussion purposes... Public Structure bob Dim SiteID As Integer Dim fred As String End Structure Public Class insCompare Implements System.Collections.Generic.IEqual...

LINQ Except using custom Comparer.

I am trying to use the "Except" method on a LINQ result set using a custom implementation if IEqualityComparer to exclude certain results based on the value of a single field from the result set. So, in simplified form I have... '' Get collection of published sites... Dim List1 = (From i In db.Sites _ Where (i.StatusID =...

IEqualityComparer for anonymous type

I have this var n = ItemList.Select(s => new { s.Vchr, s.Id, s.Ctr, s.Vendor, s.Description, s.Invoice }).ToList(); n.AddRange(OtherList.Select(s => new { s.Vchr, s.Id, s.Ctr, s.Vendor, s.Description, s.Invoice }).ToList();); I would like to do this if it where allowed n = n.Distinct((x, y) => x.Vchr == y.Vchr)).ToList(); I tried...

Is there a way to find an object's properties in List<T> using Contains?

Hi guys I was wandering how can I find out if an object already exists in my List. I'm adding "newPerson" (instance of Person class) in a List, but checking if newPerson contents/properties exists or not in the List. This piece works fine: List<Person> people = this.GetPeople(); if (people.Find(p => p.PersonID == newP...

IEqualityComparer GethashCode method

Hi, My datatable contains the following columns... resultsView.Columns.Add("Fam.", typeof(string)); resultsView.Columns.Add("Component", typeof(int)); resultsView.Columns.Add("Description", typeof(string)); resultsView.Columns.Add("Quantity", typeof(double)); resultsView.Columns.Add("Unit", typeof(string)); resu...

IEnumerable.Except() and a custom comparer

Hello, I'm having troubles with the Except() method. Instead returning the difference, it returns the original set. I've tried by implementing the IEquatable and IEqualityComparer in the Account. I've also tried creating a seperate IEqualityComparer class for Account. When the Except() method is called from main, it doesn't seem to ca...

Hashsets and different instances of class with all members identical

Let's say I have a class called myclass. In my code I have two instances of myclass, myclass1 and myclass2. Everything about them is (public and private) properties are identical. If I try to add both of them to a HashSet will it add both or only one? If it adds both and I don't want it to, can I overidde equals in the myclass definiti...

Distinct not working with LINQ to Objects

class Program { static void Main(string[] args) { List<Book> books = new List<Book> { new Book { Name="C# in depth", Authors = new List<Author> { new Author { FirstName = "Jon"...

Hashtables (Dictionary etc) with integer keys

I've been puzzling over this for a few days... feel free to shoot down any of my assumptions. We're using a Dictionary with integer keys. I assume that the value of the key in this case is used directly as the hash. Does this mean (if the keys are grouped over a small range) that the distribution of the key hash (same as the key itself,...

C# - List<T>.Remove() always deletes the first object on the list

Working in Visual Studio 2008 (C#)... I use a List collection to store instances of my custom class (Shift). I want to delete a certain shift from the list by using the Remove method. But List.Remove() always deletes the first item it finds. I've implemented the IComparable interface for my Shift, I thought this would be enough, then...

Remove duplicates from DataTable and custom IEqualityComparer<DataRow>

How have I to implement IEqualityComparer<DataRow> to remove duplicates rows from a DataTable with next structure: ID primary key, col_1, col_2, col_3, col_4 The default comparer doesn't work because each row has it's own, unique primary key. How to implement IEqualityComparer<DataRow> that will skip primary key and compare only data...

Can this implementation of an IEqualityComparer be improved?

I don't see any problems with this code, but it feels like I'm missing something. Maybe it is possible to reduce the number of lines. Or is there even a bug to be fixed? I'm open to any suggestions. public class NameComparer : IEqualityComparer<FileInfo> { public bool Equals (FileInfo x, FileInfo y) { if (x == null) { return y ==...

Is there a built-in IEqualityComparer that compares objects only using their hash value?

Is there a built-in IEqualityComparer that compares objects by the value returned by their GetHashCode value? It's easy to write, but I'd prefer to use a provided class instead of a custom one. Current code: private class HashComparer : IEqualityComparer<TKey> { private readonly Func<TKey, int> _Hasher; public HashComparer (Func...

Built-in IEqualityComparer<T> that uses ReferenceEquals

Is there a default IEqualityComparer implementation that uses ReferenceEquals? EqualityComparer<T>.Default uses ObjectComparer, which uses object.Equals(). In my case, the objects already implement IEquatable, which I need to ignore and compare by object's reference only. Thanks! ...

How to Implement IEqualityComparer<PointF> With Tolerance

This question is similar to the one here. We all know what PointF is, don't we? This is the data structure: public struct PointF { public float X; public float Y; } How to implement IEqualityComparer<PointF> with tolerance? Let's say my Equals code is like this public const float Epsilon = 0.01; //say public bool Equals(PointF ...

Using an IEqualityComparer with a LINQ to Entities Except clause

I have an entity that I'd like to compare with a subset and determine to select all except the subset. So, my query looks like this: Products.Except(ProductsToRemove(), new ProductComparer()) The ProductsToRemove() method returns a List<Product> after it performs a few tasks. So in it's simplest form it's the above. The ProductCompa...