iequalitycomparer

IEqualityComparer for Annoymous Type

隐身守侯 提交于 2021-02-06 12:27:22
问题 Firstly I have seen IEqualityComparer for anonymous type and the answers there do not answer my question, for the obvious reason that I need an IEqualityComparer not and IComparer for use with Linq's Distinct() method. I have checked the other answers too and these fall short of a solution... The Problem I have some code to manipulate and pull records in from a DataTable var glext = m_dtGLExt.AsEnumerable(); var cflist = (from c in glext orderby c.Field<string>(m_strpcCCType), c.Field<string>

IEqualityComparer for Annoymous Type

那年仲夏 提交于 2021-02-06 12:27:14
问题 Firstly I have seen IEqualityComparer for anonymous type and the answers there do not answer my question, for the obvious reason that I need an IEqualityComparer not and IComparer for use with Linq's Distinct() method. I have checked the other answers too and these fall short of a solution... The Problem I have some code to manipulate and pull records in from a DataTable var glext = m_dtGLExt.AsEnumerable(); var cflist = (from c in glext orderby c.Field<string>(m_strpcCCType), c.Field<string>

HashSet<T>.CreateSetComparer() cannot specify IEqualityComparer<T>, is there an alternative?

╄→гoц情女王★ 提交于 2020-08-08 04:06:10
问题 In the internal source there is such a constructor public HashSetEqualityComparer(IEqualityComparer<T> comparer) but it's internal so I can't use it. By default, HashSet<T>.CreateSetComparer() just uses the parameterless constructor which will apply EqualityComparer<T>.Default . Is there a way to get a HashSetEqualityComparer<T> with a IEqualityComparer<T> of choice, without copying out the code from the source? 回答1: I think best solution is using SetEquals . It does the job you need and

Which IEqualityComparer is used in a Dictionary?

可紊 提交于 2020-02-22 22:10:21
问题 Lets say I instantiate a dictionary like this var dictionary = new Dictionary<MyClass, SomeValue>(); And MyClass is my own class that implements an IEqualityComparer<> . Now, when I do operations on the dictionary - such as Add, Contains, TryGetValue etc - does dictionary use the default EqualityComparer<T>.Default since I never passed one into the constructor or does it use the IEqualityComparer that MyClass implements? Thanks 回答1: It will use the default equality comparer. If an object is

HashCode on decimal with IEqualityComparer in a ConcurrentDictionary

这一生的挚爱 提交于 2020-02-08 06:53:09
问题 I made a class to be used as a key in a dictionary. public class FourUintsOneDecimalKeyInfo { public uint IdOne { get; set; } public uint IdTwo { get; set; } public uint IdThree { get; set; } public uint IdFour { get; set; } public decimal DefinitionOne { get; set; } public class EqualityComparerAssetTableInfo : IEqualityComparer<FourUintsOneDecimalKeyInfo> { public bool Equals(FourUintsOneDecimalKeyInfo x, FourUintsOneDecimalKeyInfo y) { return x.IdOne == y.IdOne && x.IdTwo == y.IdTwo && x

If getHashCode() for string or integer is not guaranteed to be unique why use it?

限于喜欢 提交于 2020-02-04 03:25:39
问题 As i wrote in the title. If its not safe to use getHashCode() in your application, why use it? (for string and integer) I want to use it to intersect methods and except metods in Linq models or create my own IEqualityCompare class. It feels like a chance - if its not 100% secure? Or have i missed something? As quoted in String.GetHashCode Method in https://docs.microsoft.com/ Important If two string objects are equal, the GetHashCode method returns identical values. However, there is not a

If getHashCode() for string or integer is not guaranteed to be unique why use it?

流过昼夜 提交于 2020-02-04 03:25:07
问题 As i wrote in the title. If its not safe to use getHashCode() in your application, why use it? (for string and integer) I want to use it to intersect methods and except metods in Linq models or create my own IEqualityCompare class. It feels like a chance - if its not 100% secure? Or have i missed something? As quoted in String.GetHashCode Method in https://docs.microsoft.com/ Important If two string objects are equal, the GetHashCode method returns identical values. However, there is not a

Using an IEqualityComparer with a LINQ to Entities Except clause

偶尔善良 提交于 2020-01-13 02:41:11
问题 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 ProductComparer() class looks like this: public class ProductComparer : IEqualityComparer<Product> { public bool Equals(Product a, Product b) { if (ReferenceEquals(a, b))

Using an IEqualityComparer with a LINQ to Entities Except clause

匆匆过客 提交于 2020-01-13 02:41:08
问题 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 ProductComparer() class looks like this: public class ProductComparer : IEqualityComparer<Product> { public bool Equals(Product a, Product b) { if (ReferenceEquals(a, b))

LINQ GroupBy on multiple ref-type fields; Custom EqualityComparer

限于喜欢 提交于 2019-12-31 03:40:51
问题 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