C# equality

Look at the following three and see if you can figure out which one works correctly for finding any single common item in a set:

1) Permissions.Intersect(permissions).Any();
2) Permissions.Any(p => permissions.Contains(p));
3) (from p in Permissions from p1 in permissions where p1.Equals(p) select p).Any();

Note that I’m trying to do comparison on the Equals method, not by reference.

If you guessed that I posted these in the order I tried, you win. The third try was the one that finally allowed me to compare two groups to find if there were any common items. I’m kind of annoyed that I had to go through these three iterations. It’s always a crapshoot whether methods will compare by reference, equality or the Equals() method.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: