0

Distinct() is not working. It displays all the repeating values. I searched for a solution but just got more confused. I tried this :

var categories = db.Orders.OrderBy(c => c.Item1).ToList().Distinct();
var categories = db.Orders.Distinct().OrderBy(c => c.Item1).ToList();

Is there a quick uncomplicated way to make this work?

Servy
  • 202,030
  • 26
  • 332
  • 449
Mike Howe
  • 263
  • 2
  • 8
  • 19
  • I guess your orders are distinct, then. Are you sure they aren't? – usr Jul 31 '14 at 14:05
  • Should work to make the entries distinct. Do you have a specific example where it doesn't? – Joachim Isaksson Jul 31 '14 at 14:06
  • Check this http://msdn.microsoft.com/en-us/library/vstudio/bb338049(v=vs.100).aspx – Deepu Madhusoodanan Jul 31 '14 at 14:07
  • ToList().Distinct() kind of makes the ToList() redundant. If you want a List, switch these around: Distinct().ToList() – Dennis_E Jul 31 '14 at 14:07
  • 2
    @Dennis_E Not really, one will likely do the comparison in the database and the other in C#. – Rawling Jul 31 '14 at 14:08
  • @MikeHowe I take it this is Linq to SQL? It isn't actually stated anywhere, just inferred. – Adam Houldsworth Jul 31 '14 at 14:08
  • 1
    http://stackoverflow.com/questions/489258/linq-distinct-on-a-particular-property?rq=1 – Vland Jul 31 '14 at 14:12
  • @Vland It may well be that is what OPs needs, it's hard to tell. Your answer could have been clearer, but I'm sorry people deleted it. – Rawling Jul 31 '14 at 14:19
  • Orders is just a SQL server table that has with entries like : Home, Home, Car, Car, etc. I'm using entity framework. This is just a practice program. – Mike Howe Jul 31 '14 at 14:21
  • @Rawling I'm pretty sure that using Groupby is more than enough to solve his problem. but the master programmers are not going to allow it as a valid answer, so be it. cy all – Vland Jul 31 '14 at 14:27
  • 1
    I used the Group By and got it to work (see updated answer). – Mike Howe Jul 31 '14 at 14:28
  • @Vland Is the valid answer to use the "MoreLINQ" DistinctBy? Should I mark my question as the answer? Can't believe Linq has to make a simple Distinct so complicated. – Mike Howe Jul 31 '14 at 14:34
  • 1
    @MikeHowe `Distinct` *isn't* complicated. it does exactly what it's supposed to do and *it's working perfectly*. You just don't' want to get distinct items, despite not discussing this in your question. You actually want to get distinct items *using a single property of that item as it's identity*. That's a very different problem. This is why you shouldn't just say, "it doesn't work" in your question but rather explain, specifically, what is happening and what you want to happen. Had you done that an answer would be trivial to provide. – Servy Jul 31 '14 at 15:00
  • If it's a table, don't you have a primary key? – Lasse V. Karlsen Jul 31 '14 at 15:01
  • 1
    I guess I didn't understand what Linq Distinct does. I was thinking it was like the regular SQL Distinct. – Mike Howe Jul 31 '14 at 15:24
  • It is like the regular SQL Distinct. `db.Orders.Distinct()` is the same as `SELECT DISTINCT * FROM orders` – Robert McKee Jul 31 '14 at 21:37

1 Answers1

3

use GroupBy instead of Distinct

Vland
  • 4,151
  • 2
  • 32
  • 43
  • I had a similar problem, he asked for a quick and not complicated solution. groupBy "some value" is quick and easy to implement. a new comparer is not that "easy" – Vland Jul 31 '14 at 14:09
  • 4
    @Downvoters In fact, `GroupBy(...).Select(g => g.First())` might work... (BTW: this is not an answer, It can only be a comment) – L.B Jul 31 '14 at 14:10
  • 1
    yes, it would work, but please go on downvoting if you feel like it – Vland Jul 31 '14 at 14:10
  • @Vland, I didn't see you answer until now for some reason. I'm confused now to say the least. Should I just delete the question? – Mike Howe Jul 31 '14 at 14:40
  • @MikeHowe you tell me. if this answer was helpful, flag as as correct. otherwise ignore it – Vland Jul 31 '14 at 14:41
  • @Vland maybe you could add some information into your answer? To show how to use it exactly or something that will be helpful for future readers? – Fedor Jul 31 '14 at 15:17