Take the 2-minute tour ×

Using the following code I am not getting records distinct on the ID. Why is this?

List<string> products = new List<string>();

var predicate = PredicateBuilder.True<Services>()
                                .And(m => products.Contains(m.Service));

var Results = from d in Services.Distinct()
                                .Where(predicate )
              select d.ID;

Database table (Services) is:

ID         Service
400        13Conv
401        13Conv
400        12Conv
400        07Conv
400        11Conv

Using the data above I get a count of 5 records when I would expect 2.

The ID field is a string in the DB.

share|improve this question
add comment

2 Answers

It's Distinct, not DistinctByKey (method like that do not exists within LINQ), so all values within the row are checked for equality. With that check non of rows are equal. That's why they are all returned by query.

By the way - how would the database could decide, which row with given Key should be returned?

If you're trying to receive IDs only, and they should be distinct try that query:

var results = Services.Where(predicate).Select(s => s.ID).Distinct().ToList();


According to question like this one there is known problem with LINQ to Entites and DISTINCT. Try that query instead:

Services.Where(predicate).GroupBy(s => s.Id).Select(g => g.Key).ToList();
share|improve this answer
Not sure I understand your last question. –  John S Mar 15 '13 at 21:15
I think me means the fact that you didn't provide information to suggest (and it can't be inferred), which Service would be the right one to include for ID=400 and ID=401. Sure you don't want a Group By instead of Distinct? –  Rich Mar 15 '13 at 21:33
I would expect it to behave the same way as SQL Distinct. I don't care about the services data I just want the unique IDs. –  John S Mar 15 '13 at 21:36
Made the changes with the ToList() and it still returns duplicates. –  John S Mar 15 '13 at 21:50
If I switch the ToList and Distinct then it works as expected... –  John S Mar 15 '13 at 21:54
show 2 more comments

You told it you wanted a distinct list of Service objects, not of service IDs (or names).

I'm not familiar with building predicates programmatically, but I think you'd do something like this:

var Results = (from d in Services.Where(Predicate)
               select d.ID).Distinct();
share|improve this answer
I've tried moving the .Distinct() to where you have it in your example and it returns the same record count. Seems logical but doesn't work. –  John S Mar 15 '13 at 21:17
What do you expect Results to be? A list of IDs? A list of Service objects? Also, try adding .ToArray() or .ToList() after the Distinct(). I'd want to be sure that the query had actually been executed. –  Ann L. Mar 15 '13 at 21:22
I would expect a list of distinct IDs and instead I am getting a list of IDs with duplicates. –  John S Mar 15 '13 at 21:33
Added a ToList() and I still get dups. –  John S Mar 15 '13 at 21:52
See note below. –  John S Mar 15 '13 at 21:54
add comment

Your Answer


