Solved

LINQ To SQL: Distinct()

Posted on 2010-09-24
16
807 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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
 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

840 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