Solved

LINQ To SQL: Distinct()

Posted on 2010-09-24
16
804 Views
Last Modified: 2012-05-10

Hi guys,

I'm trying to retrieve a collection of distinct users from my database.
var DistinctUsers = AllUsers.Distinct(new UserComparer());

Open in new window

AllUsers is an IQueryable<UserEntity> and has multiple entries for the same user. Every user has a unique ID, but there are other fields which are not identical in AllUsers. So, I came up with this simple IEqualityComparer object, which relies on the comparision of user IDs.
public class UserComparer : IEqualityComparer<UserEntity>
    {
        public bool Equals(UserEntity x, UserEntity y)
        {
            return (x.Id == y.Id);
        }
        public int GetHashCode(UserEntity x)
        {
            return (x.Id.GetHashCode());
        }
    }

Open in new window

However, this still does not work. I'm still getting redundant users in my DistinctUsers collection.

Any ideas? Am I missing something?
0
Comment
Question by:softwarea
  • 6
  • 4
  • 4
  • +1
16 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 33751661
Put a breakpoint in the 'Equals' function - is it being called ?
0
 

Author Comment

by:softwarea
ID: 33751693
Hi Andy,

how did you figure THIS? Damn, it is NOT called.
I had not even thought about checking this.

Any idea why the comparer is not called? I probably missed someting totally stupid...
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 250 total points
ID: 33751702
>>how did you figure THIS?

Because the function should work IF it was being called, as it didn't work then ....
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 33751715
As to why it isn't being called?  You need to look at what should be calling it - is that function itself actually being called?
0
 

Author Comment

by:softwarea
ID: 33751733
Yes!
[code]var DistinctUsers = AllUsers.Distinct(new UserComparer());[/code]
This line is definitely called.

And AllUsers contains all the results which I would expect. However, there are redundant entries.
0
 
LVL 5

Assisted Solution

by:VincentSG
VincentSG earned 250 total points
ID: 33751754
Hi!

Try modifying your code to
var DistinctUsers = AllUsers.ToList().Distinct(new UserComparer());
0
 

Author Comment

by:softwarea
ID: 33751766
Hi Vincent,

unbelievable. I had just tried that a second ago on my own.
And yes, now it is working!

Perfect!

Are you guys ok, if I spilt the points?
0
 
LVL 14

Expert Comment

by:existenz2
ID: 33751771
Turning the code around would better actually. If you put .ToList() as last it will first execute the distinct and then convert it to a list, which should ensure that the Distinct is definatly called.
var DistinctUsers = AllUsers.Distinct(new UserComparer()).ToList();

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 5

Expert Comment

by:VincentSG
ID: 33751775
Hi!
Great that it helps. Sure go ahead.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 33751798
yes, split is OK
0
 

Accepted Solution

by:
softwarea earned 0 total points
ID: 33751800
I just tried both versions:


1. var DistinctUsers = AllUsers.ToList().Distinct(new UserComparer());
2. var DistinctUsers = AllUsers.Distinct(new UserComparer()).ToList();


No. 1 works.
No. 2 does not!
0
 
LVL 5

Expert Comment

by:VincentSG
ID: 33751822
Hi softwarea!

I am not sure whether this is a problem with Linq (but I think it is).

When you tried to call Distinct to the IQueryable<UserEntity> AllUsers, the data has not actually arrived yet. Thus you UserComparer method is not called.

When you called to ToList() method, you are actually fetching the data and then feeding them to the UserComparer method.

I agreed with existenz2 that it may not be efficient but I have yet to find a better solution.
0
 

Author Comment

by:softwarea
ID: 33751844
I think the same.

LINQ queries rely heavily on lazy evaluation. I just didn't know that you have to execute the query first (by calling ToList()) and then apply the Distinct() operator.
The other way around makes more sense on first view, though. But how knows... :-)

Thanks for all your help guys!  
0
 
LVL 14

Expert Comment

by:existenz2
ID: 33751892
It indeed seems a LINQ problem, because of the following:
1. var DistinctUsers = AllUsers.ToList().Distinct(new UserComparer());
2. var DistinctUsers = AllUsers.Distinct(new UserComparer()).ToList();


#1 first executes the previous lazy LINQ queries and return an IList on which it will execute the distinct (which does directly execute it again).
#2 This should execute the lazy LINQ query before executing the Distinct (or while executing the distinct) and then cast the result to an IList.

Both cases should work perfectly in theory for LINQ, only #2 should be a bit faster. But at least it is solved.
0
 
LVL 5

Expert Comment

by:VincentSG
ID: 33766853
Hi softwarea,
Could you try to signal Close Request again? Somehow this question is still not closed.
0
 

Author Comment

by:softwarea
ID: 33767221
Hi Vincent,

the thread will be closed automatically by tomorrow.

Thanks!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
A simple description of email encryption using a secure portal service. This is one of the choices offered by The Email Laundry for email encryption. The other choices are pdf encryption which creates an encrypted pdf of your email and any attachmen…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now