473,394 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

[SOLVED] IEnumerable.Except() and a custom comparer

1
Hello, I'm having troubles with the Except() method.
Instead returning the difference, it returns the original set.

I've tried by implementing the IEquatable and IEqualityComparer in the Account.
I've also tried creating a seperate IEqualityComparer class for Account.

When the Except() method is called from main, it doesn't seem to call my custom Equals() method, but when I tried Count(), it did call the custom GetHashCode() method!

I'm sure I made a trivial mistake somewhere and I hope a fresh pair of eyes can help me.

main:
Expand|Select|Wrap|Line Numbers
  1. IEnumerable<Account> everyPartnerID = 
  2.     from partner in dataContext.Partners
  3.     select new Account { IDPartner = partner.ID, Name = partner.Name };
  4.  
  5.  
  6. IEnumerable<Account> hasAccountPartnerID = 
  7.     from partner in dataContext.Partners
  8.     from account in dataContext.Accounts
  9.     where
  10.         !partner.ID.Equals(Guid.Empty) &&
  11.         account.IDPartner.Equals(partner.ID) &&
  12.         account.Username.Equals("Special")
  13.     select new Account { IDPartner = partner.ID, Name = partner.Name };
  14.  
  15. IEnumerable<Account> noAccountPartnerID = 
  16.     everyPartnerID.Except(
  17.         hasAccountPartnerID, 
  18.         new LambdaComparer<Account>((x, y) => x.IDPartner.Equals(y.IDPartner)));           
  19.  
Account:
Expand|Select|Wrap|Line Numbers
  1.     public class Account : IEquatable<Account>
  2.     {
  3.         public Guid IDPartner{ get; set; }
  4.         public string Name{ get; set; }
  5.  
  6. /*        #region IEquatable<Account> Members
  7.  
  8.         public bool Equals(Account other)
  9.         {
  10.             return this.IDPartner.Equals(other.IDPartner);
  11.         }
  12.  
  13.         #endregion*/
  14.     }
LambdaComparer:
Expand|Select|Wrap|Line Numbers
  1.     public class LambdaComparer<T> : IEqualityComparer<T>
  2.     {
  3.         private readonly Func<T, T, bool> _lambdaComparer;
  4.         private readonly Func<T, int> _lambdaHash;
  5.  
  6.         public LambdaComparer(Func<T, T, bool> lambdaComparer) :
  7.             this(lambdaComparer, o => o.GetHashCode())
  8.         {
  9.         }
  10.  
  11.         public LambdaComparer(Func<T, T, bool> lambdaComparer, Func<T, int> lambdaHash)
  12.         {
  13.             if (lambdaComparer == null)
  14.                 throw new ArgumentNullException("lambdaComparer");
  15.             if (lambdaHash == null)
  16.                 throw new ArgumentNullException("lambdaHash");
  17.  
  18.             _lambdaComparer = lambdaComparer;
  19.             _lambdaHash = lambdaHash;
  20.         }
  21.  
  22.         public bool Equals(T x, T y)
  23.         {
  24.             return _lambdaComparer(x, y);
  25.         }
  26.  
  27.         public int GetHashCode(T obj)
  28.         {
  29.             return _lambdaHash(obj);
  30.         }
  31.     }
Aug 19 '09 #1
0 1604

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Bob Rundle | last post by:
I'm trying to serialize a class with XmlSerializer. This class implements the IEnumerable interface. I implemented the IEnumerable interface for reasons other than Xml serialization. However I...
3
by: MattC | last post by:
Hi, I found this code somewhere on the net and need to make some alterations. public class GenericSorter : IComparer { String sortProperty; bool sortOrder; public GenericSorter(String...
0
by: Brian Henry | last post by:
Ok I've never implemented a snap location before so I dont really know what im doing wrong here... anyways, I am making a custom slider control that takes dates as its values instead of integers......
13
by: David | last post by:
Hi all, I have a singleton ienumerable that collects data from a database. I have listed the code below. It has the usual methods of current, move and reset. I need to go to a certain position...
5
by: Shikari Shambu | last post by:
Hi, I am trying to implement a collection that implements IEnumerable<T>. I keep getting the following error 'IEnumerator<...>.Current' in explicit interface declaration is not a member of...
2
by: =?Utf-8?B?a2VubmV0aEBub3NwYW0ubm9zcGFt?= | last post by:
When creating multiple iterators, the original is defined as returning IEnumerator, ie public IEnumerator GetEnumerator() { yield x; ...} whereas the additional ones are defined as returning...
2
by: Luc The Perverse | last post by:
Hello! I am trying to find a way to deal with Windows' case insensitivity without just forcing everything lowercase. While I am open to criticism on my method - my question relates to the...
6
by: Tony | last post by:
Hello! My first question: I just can't figure out what is the usefulness of Comparer.Default.Compare(somestring1, somestring2); because I can just the same use...
10
by: Tony | last post by:
Hello! I'm reading in a book and this can't be correct it says. "Objects passed to Comparer.Compare() are checked to see if they support IComparable. If they do, then that implementation is...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.