iequalitycomparer

Pass a lambda expression in place of IComparer or IEqualityComparer or any single-method interface?

不想你离开。 提交于 2019-11-27 11:52:48
问题 I happened to have seen some code where this guy passed a lambda expression to a ArrayList.Sort(IComparer here) or a IEnumerable.SequenceEqual(IEnumerable list, IEqualityComparer here) where an IComparer or an IEqualityComparer was expected. I can't be sure if I saw it though, or I am just dreaming. And I can't seem to find an extension on any of these collections that accepts a Func<> or a delegate in their method signatures. Is there such an overload/extension method? Or, if not, is it

Using IEqualityComparer GetHashCode with a tolerance

*爱你&永不变心* 提交于 2019-11-27 07:10:00
问题 I am trying to implement an IEqualityComparer that has a tolerance on a date comparison. I have also looked into this question. The problem is that I can't use a workaround because I am using the IEqualityComparer in a LINQ .GroupJoin() . I have tried a few implementations that allow for tolerance. I can get the Equals() to work because I have both objects but I can't figure out how to implement GetHashCode() . My best attempt looks something like this: public class ThingWithDateComparer :

What's the role of GetHashCode in the IEqualityComparer<T> in .NET?

岁酱吖の 提交于 2019-11-27 02:54:53
I'm trying to understand the role of the GetHashCode method of the interface IEqualityComparer. The following example is taken from MSDN: using System; using System.Collections.Generic; class Example { static void Main() { try { BoxEqualityComparer boxEqC = new BoxEqualityComparer(); Dictionary<Box, String> boxes = new Dictionary<Box, string>(boxEqC); Box redBox = new Box(4, 3, 4); Box blueBox = new Box(4, 3, 4); boxes.Add(redBox, "red"); boxes.Add(blueBox, "blue"); Console.WriteLine(redBox.GetHashCode()); Console.WriteLine(blueBox.GetHashCode()); } catch (ArgumentException argEx) { Console

IEqualityComparer<T> that uses ReferenceEquals

醉酒当歌 提交于 2019-11-27 01:23:14
Is there a default IEqualityComparer<T> implementation that uses ReferenceEquals ? EqualityComparer<T>.Default uses ObjectComparer, which uses object.Equals() . In my case, the objects already implement IEquatable<T> , which I need to ignore and compare by object's reference only. Yurik Just in case there is no default implementation, this is my own: Edit by 280Z28: Rationale for using RuntimeHelpers.GetHashCode(object) , which many of you probably haven't seen before. :) This method has two effects that make it the correct call for this implementation: It returns 0 when the object is null.

List.Contains is not working as hoped

我们两清 提交于 2019-11-26 23:17:31
问题 If I have an object of type MyBull and a List<MyBull> orig : // Just an example MyBull x = getMeTheObjectWithIdFromDB(9); orig.add(x); // Again same? data object MyBull y = getMeTheObjectWithIdFromDB(9); Why is this false then? // This is false, even though all the properties // of x and y are the same. orig.Contains<MyBull>(y); 回答1: By default objects will expose reference based equality. If you want custom rules, such as equality based on id fields, you need to override the Equals and

IEqualityComparer for SequenceEqual

一世执手 提交于 2019-11-26 22:40:26
In C#, is there a IEqualityComparer<IEnumerable> that uses the SequenceEqual method to determine equality? Cédric Bignon There is no such comparer in .NET Framework, but you can create one: public class IEnumerableComparer<T> : IEqualityComparer<IEnumerable<T>> { public bool Equals(IEnumerable<T> x, IEnumerable<T> y) { return Object.ReferenceEquals(x, y) || (x != null && y != null && x.SequenceEqual(y)); } public int GetHashCode(IEnumerable<T> obj) { // Will not throw an OverflowException unchecked { return obj.Where(e => e != null).Select(e => e.GetHashCode()).Aggregate(17, (a, b) => 23 * a +

How to use the IEqualityComparer

我的未来我决定 提交于 2019-11-26 20:14:42
I have some bells in my database with the same number. I want to get all of them without duplication. Then I create a compare class to do this work, but the execution of the function makes a big delay from the function without distinct, from 0.6 sec to 3.2 sec! Am I doing it right or I have to use another method? reg.AddRange((from a in this.dataContext.reglements join b in this.dataContext.Clients on a.Id_client equals b.Id where a.date_v <= datefin && a.date_v >= datedeb where a.Id_client == b.Id orderby a.date_v descending select new Class_reglement { nom = b.Nom, code = b.code, Numf = a

IEqualityComparer for anonymous type

有些话、适合烂在心里 提交于 2019-11-26 19:49:37
问题 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 using the generic LambdaComparer but since im using anonymous types there is no type associate it with. "Help me Obi Wan Kenobi, you're my only hope" 回答1: The trick

What is the difference between IEqualityComparer<T> and IEquatable<T>?

一个人想着一个人 提交于 2019-11-26 15:15:54
问题 I want to understand the scenarios where IEqualityComparer<T> and IEquatable<T> should be used. The MSDN documentation for both looks very similar. 回答1: IEqualityComparer<T> is an interface for an object that performs the comparison on two objects of the type T . IEquatable<T> is for an object of type T so that it can compare itself to another. 回答2: When deciding whether to use IEquatable<T> or IEqualityComparer<T>, one could ask: Is there a preferred way of testing two instances of T for

writing a custom comparer for linq groupby

百般思念 提交于 2019-11-26 14:48:45
问题 Again this sample is a very simplified version of my actual problem involving a custom comparer for linq grouping. What have I done wrong? The code below produces the result below (1.2, 0), (4.1, 0), (4.1, 0), (1.1, 0), however I was expecting the following since 1.1 and 1.2 are < 1.0 apart. (1.2, 0), (1.1, 0), (4.1, 0), (4.1, 0), class Program { static void Main(string[] args) { IEnumerable<Point> points = new List<Point> { new Point(1.1, 0.0) , new Point(4.1, 0.0) , new Point(1.2, 0.0) ,