iequalitycomparer

Checking for equality in Objective-C

人盡茶涼 提交于 2019-11-26 14:28:44
问题 How do i check the key in dictionary is same as the string in method parameter? i.e in below code , dictobj is NSMutableDictionary's object , and for each key in dictobj i need to compare with string. How to achieve this ? Should i typecase key to NSString?? -(void)CheckKeyWithString:(NSString *)string { //foreach key in NSMutableDictionary for(id key in dictobj) { //Check if key is equal to string if(key == string)// this is wrong since key is of type id and string is of NSString,Control

How to implement IEqualityComparer to return distinct values?

≡放荡痞女 提交于 2019-11-26 13:47:58
问题 I have a L2E query that returns some data that contains duplicate objects. I need to remove those duplicate objects. Basically I should assume that if their IDs are the same then the objects are duplicate. I've tried q.Distinct() , but that still returned duplicate objects. Then I've tried implementing my own IEqualityComparer and passing it to the Distinct() method. The method failed with following text: LINQ to Entities does not recognize the method 'System.Linq.IQueryable 1[DAL.MyDOClass]

What&#39;s the role of GetHashCode in the IEqualityComparer<T> in .NET?

ぐ巨炮叔叔 提交于 2019-11-26 12:36:17
问题 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

IEqualityComparer for SequenceEqual

删除回忆录丶 提交于 2019-11-26 08:24:30
问题 In C#, is there a IEqualityComparer<IEnumerable> that uses the SequenceEqual method to determine equality? 回答1: 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 {

IEqualityComparer<T> that uses ReferenceEquals

不羁岁月 提交于 2019-11-26 08:21:50
问题 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. 回答1: 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

What problem does IStructuralEquatable and IStructuralComparable solve?

吃可爱长大的小学妹 提交于 2019-11-26 07:28:39
问题 I\'ve noticed these two interfaces, and several associated classes, have been added in .NET 4. They seem a bit superfluous to me; I\'ve read several blogs about them, but I still can\'t figure out what problem they solve that was tricky before .NET 4. What use are IStructuralEquatable and IStructuralComparable ? 回答1: All types in .NET support the Object.Equals() method which, by default, compares two types for reference equality . However, sometimes, it also desirable to be able to compare

How to use the IEqualityComparer

左心房为你撑大大i 提交于 2019-11-26 06:36:16
问题 I have some bells in my database with the same number. I want to get all of them without duplication. I created a compare class to do this work, but the execution of the function causes a big delay from the function without distinct, from 0.6 sec to 3.2 sec! Am I doing it right or do 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

Distinct not working with LINQ to Objects

我们两清 提交于 2019-11-26 03:15:51
问题 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\", LastName=\"Skeet\" }, new Author { FirstName = \"Jon\", LastName=\"Skeet\" }, } }, new Book { Name=\"LINQ in Action\", Authors = new List<Author> { new Author { FirstName = \"Fabrice\", LastName=\"Marguerie\" }, new Author { FirstName = \"Steve\", LastName=\"Eichert\" }, new Author { FirstName = \"Jim\", LastName=