-5

I have a List<Object> n Objects. In n Objects have some object that they have same Id. Example List<T> = {t1, t2, t3, t4, t5....,t10}.

assuming that:

t1.Id = t5.id

So i want to remove t5 from that List.

Who can help me plz.

In this question: using LINQ to remove objects within a List you know that object duplicates (ex: "bob"), but in my question, in this List, i don't know any object duplicate before.

Community
  • 1
  • 1
Sonrobby
  • 3,112
  • 8
  • 30
  • 38
  • 2
    Did you tried _anything_ to solve your problem? Show your effort first.. – Soner Gönül Apr 05 '13 at 08:34
  • You might want to check out the [`System.Collections.Generic.Dictionary` class](http://msdn.microsoft.com/en-us/library/xfhwa508.aspx), unless your homework/assignment absolutely requires you to use `List` instead. – Nolonar Apr 05 '13 at 08:40
  • i'm new with LINQ. I tried with 2 for loop, but this solution not optimize. – Sonrobby Apr 05 '13 at 08:51
  • If you need an optimal solution: This kind of problem is best solved with a `Dictionary` (as stated above). A `Dictionary` does not allow you to add multiple data with the same key and is extremely fast at finding a key. – Nolonar Apr 05 '13 at 08:56

4 Answers4

8

Using LINQ to Objects only:

source = source.GroupBy(t => t.id).Select(g => g.First()).ToList();
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • +1 from me. Also correct, if you aren't lazy enough to use existing implementation) – Sergey Berezovskiy Apr 05 '13 at 08:37
  • @lazyberezovsky I think it may be an overkill to load whole `moreLinq` assemble just for one `DistinctBy` call. That's why I posted the LINQ to Objects only answer first. – MarcinJuraszek Apr 05 '13 at 08:40
  • Wow. You are right that there is no Distinct(predicate). Of course there should be. And the reason I thought there was is of course because I created my own :D go figure. – Anders Forsgren Apr 05 '13 at 08:41
6

You can use moreLinq (get it from NuGet) extension DistinctBy

var result = list.DistinctBy(x => x.Id).ToList();
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • What is the reason for downvote? – Sergey Berezovskiy Apr 05 '13 at 08:39
  • I didn't downvote, but there is no reason that `Distinct` will pick the first, as it is implemented using a hash set or similar. Since he specifically asked `t5` to be removed, you need to guarantee that. That was my initial reaction anyway. Dunno if that is why you were downvoted. – Lasse V. Karlsen Apr 05 '13 at 08:40
  • Actually, that is not true for `DistinctBy`. Perhaps that wasn't the reason for the down vote then. – Lasse V. Karlsen Apr 05 '13 at 08:41
  • 1
    @LasseV.Karlsen [DistnctBy](http://code.google.com/p/morelinq/source/browse/MoreLinq/DistinctBy.cs?r=d4396b9ff63932be0ab07c36452a481d20f96307) described as `If a key is seen multiple times, only the first element with that key is returned.` – Sergey Berezovskiy Apr 05 '13 at 08:42
  • @LasseV.Karlsen You can look at [`DistinctBy` implementation](https://code.google.com/p/morelinq/source/browse/MoreLinq/DistinctBy.cs) and you'll see it will return the first one. – MarcinJuraszek Apr 05 '13 at 08:43
  • Yes, I went and looked at the source, you're right. – Lasse V. Karlsen Apr 05 '13 at 08:43
0

Try this:

list.RemoveAll(input => list.Count(i => i.Id == input.Id) > 1);
Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
0
var res = items.GroupBy(item => item.Id).Select(item2 => item2.First()).ToList();
KF2
  • 9,887
  • 8
  • 44
  • 77