iequalitycomparer

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

谁说胖子不能爱 提交于 2019-11-28 19:04:36
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 possible to muck around like this and pass an algorithm (read delegate) where a single-method interface is

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

試著忘記壹切 提交于 2019-11-28 18:00:29
问题 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 cars are the same color as another in the list. Here are the cars in the list and their color property: Car1.Color = Red; Car2.Color = Blue; Car3.Color = Green;

Using IEqualityComparer GetHashCode with a tolerance

十年热恋 提交于 2019-11-28 12:46:33
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 : IEqualityComparer<IThingWithDate> { private readonly int _daysToAdd; public ThingWithDateComparer(int

Using IEqualityComparer for Union

余生长醉 提交于 2019-11-27 23:31:42
问题 I simply want to remove duplicates from two lists and combine them into one list. I also need to be able to define what a duplicate is. I define a duplicate by the ColumnIndex property, if they are the same, they are duplicates. Here is the approach I took: I found a nifty example of how to write inline comparers for the random occassions where you need em only once in a code segment. public class InlineComparer<T> : IEqualityComparer<T> { private readonly Func<T, T, bool> getEquals; private

List.Contains is not working as hoped

与世无争的帅哥 提交于 2019-11-27 23:18:37
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); 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 GetHashCode methods. If you can use LINQ then you can class Vessel { public int id { get; set; } public string

IEqualityComparer for anonymous type

十年热恋 提交于 2019-11-27 19:19:34
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" JaredPar The trick is to create a comparer that only works on inferred types. For instance: public class Comparer<T> :

How to use linq `Except` with multiple properties with different class?

微笑、不失礼 提交于 2019-11-27 18:57:04
问题 I am trying to learn the Linq/Lambda expressions and was stuck at somewhere. What I was Doing I have created two classes with properties which have some common properties in them. The classes are like(It's test code). class TestA { public int Id { get; set; } public int ProductID { get; set; } public string Category { get; set; } public TestA(int id, int procid, string category) { this.Id = id; this.ProductID = procid; this.Category = category; } } class TestB { public int ProductID { get;

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

十年热恋 提交于 2019-11-27 17:13:43
I want to understand the scenarios where IEqualityComparer<T> and IEquatable<T> should be used. The MSDN documentation for both looks very similar. 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. 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 equality, or are there several equally valid ways? If there is only one way of testing two instances of T for

writing a custom comparer for linq groupby

跟風遠走 提交于 2019-11-27 16:06:33
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) , new Point(4.1, 0.0) }; foreach (var group in points.GroupBy(p => p, new PointComparer())) { foreach (var

Linq Except with custom IEqualityComparer

删除回忆录丶 提交于 2019-11-27 12:03:30
问题 I am trying to find the difference between two generic lists, as in the example below. Even though t1 and t2 contain the same properties, they are not the same object, so I have need to implement an IEqualityComparer. This appears to be working with this example, but the real class has several other properties and I also need to do the same with a few other class. So I was wondering if I am re-inventing the wheel? Is there an easier method of comparing all the properties of two objects? At