iequalitycomparer

LINQ GroupBy on multiple ref-type fields; Custom EqualityComparer

≡放荡痞女 提交于 2019-12-02 00:04:57
So I've looked through about 20 examples on this on SO and elsewhere, but haven't found one which covers what I'm trying to do. This - Can I specify my explicit type comparator inline? - looks like what I need, but doesn't go far enough (or I don't understand how to take it further). I have a List of LoadData, the LoadData object has fields of both reference and value types Need to group on a mixture of ref and value fields, project the output to an anonymous type Need (I think) to provide a custom IEqualityComparer to specify how to compare the GroupBy fields, but they are an anonymous type

How do you create a dynamic equality implementation where you can pass in the property names to be compared?

若如初见. 提交于 2019-12-01 11:14:10
Say I have an object Person with the properties below: public class Person { public int ID { get; set; } public int EmployeeNo { get; set; } public string JobDescription { get; set; } public string Code { get; set; } } How would I dynamically check the equality of specific properties by name? eg. var dynamicEqualityComparer = RetrieveDynamicEqualityComparer("ID", "JobDescription"); var intersectedPersons = listOfPerson1.Intersect(listOfPerson2, dynamicEqualityComparer); The above snippit would use the default linq intersect method using the dynamically generated equality comparison method

How do you create a dynamic equality implementation where you can pass in the property names to be compared?

僤鯓⒐⒋嵵緔 提交于 2019-12-01 09:39:18
问题 Say I have an object Person with the properties below: public class Person { public int ID { get; set; } public int EmployeeNo { get; set; } public string JobDescription { get; set; } public string Code { get; set; } } How would I dynamically check the equality of specific properties by name? eg. var dynamicEqualityComparer = RetrieveDynamicEqualityComparer("ID", "JobDescription"); var intersectedPersons = listOfPerson1.Intersect(listOfPerson2, dynamicEqualityComparer); The above snippit

Hashtables (Dictionary etc) with integer keys

六眼飞鱼酱① 提交于 2019-12-01 04:51:42
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, right?) will be in a similarly small range, and therefore a bad choice for a hashtable? Would it be better to provide an IEqualityComparer that did something clever with primes and modulo mathematics to calculate a better distributed hash? It's not used

IEqualityComparer not working as intended

╄→尐↘猪︶ㄣ 提交于 2019-12-01 03:18:57
I have a List of paths of files stored on my computer. My aim is to first filter out the files which have the same name and and then filter out those which have the same size. To do so, I have made two classes implementing IEqualityComparer<string> , and implemented Equals and GetHashCode methods. var query = FilesList.Distinct(new CustomTextComparer()) .Distinct(new CustomSizeComparer()); The code for both of the classes is given below:- public class CustomTextComparer : IEqualityComparer<string> { public bool Equals(string x, string y) { if (Path.GetFileName(x) == Path.GetFileName(y)) {

Hashtables (Dictionary etc) with integer keys

烂漫一生 提交于 2019-12-01 01:10:55
问题 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, right?) will be in a similarly small range, and therefore a bad choice for a hashtable? Would it be better to provide an IEqualityComparer that did something

IEqualityComparer not working as intended

人盡茶涼 提交于 2019-12-01 00:14:09
问题 I have a List of paths of files stored on my computer. My aim is to first filter out the files which have the same name and and then filter out those which have the same size. To do so, I have made two classes implementing IEqualityComparer<string> , and implemented Equals and GetHashCode methods. var query = FilesList.Distinct(new CustomTextComparer()) .Distinct(new CustomSizeComparer()); The code for both of the classes is given below:- public class CustomTextComparer : IEqualityComparer

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

无人久伴 提交于 2019-11-29 21:02:17
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; Car4.Color = Red; Car5.Color = Red; For this example I need the result (IEnumerable<>, List<>, or

Using IEqualityComparer for Union

孤人 提交于 2019-11-29 05:57:22
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 readonly Func<T, int> getHashCode; public InlineComparer(Func<T, T, bool> equals, Func<T, int> hashCode

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

南笙酒味 提交于 2019-11-29 04:50:17
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; set; } public string Category { get; set; } public TestB(int procid, string category) { this.ProductID =