LINQ: Remove first entry of groups in a list
-
Well ,that title is confusing... ;P I have this list:
var list = new List<(int Key, string Value)>();
list.Add((1, "foo1"));
list.Add((1, "foo2"));
list.Add((1, "foo3"));
list.Add((1, "foo4"));
list.Add((2, "bar1"));
list.Add((2, "bar2"));
list.Add((2, "bar3"));
list.Add((2, "bar4"));
list.Add((3, "baz"));From that list I want a new list:
(1, "foo2")
(1, "foo3")
(1, "foo4")
(2, "bar2")
(2, "bar3")
(2, "bar4")Notcie the first occurances of any "key/group" are removed in the result list. These entries are removed:
(1, "foo1"), (2, "bar1") and (3, "baz")
I have no clue how to do this. I have tried to create a solution, but I don't know how to find the first entry of a group and remove it using LINQ. The.Distinct()
and.First()
methods seems needed in someway, but I cannot figure it out. I'm sure I can hack something ugly with foreach loops and new lists, but I would prefer a solution with LINQ. Any suggestions for a solution? Best regards -
Well ,that title is confusing... ;P I have this list:
var list = new List<(int Key, string Value)>();
list.Add((1, "foo1"));
list.Add((1, "foo2"));
list.Add((1, "foo3"));
list.Add((1, "foo4"));
list.Add((2, "bar1"));
list.Add((2, "bar2"));
list.Add((2, "bar3"));
list.Add((2, "bar4"));
list.Add((3, "baz"));From that list I want a new list:
(1, "foo2")
(1, "foo3")
(1, "foo4")
(2, "bar2")
(2, "bar3")
(2, "bar4")Notcie the first occurances of any "key/group" are removed in the result list. These entries are removed:
(1, "foo1"), (2, "bar1") and (3, "baz")
I have no clue how to do this. I have tried to create a solution, but I don't know how to find the first entry of a group and remove it using LINQ. The.Distinct()
and.First()
methods seems needed in someway, but I cannot figure it out. I'm sure I can hack something ugly with foreach loops and new lists, but I would prefer a solution with LINQ. Any suggestions for a solution? Best regardsTry:
var x = list.GroupBy(g => g.Key).Select(grp => grp.Skip(1)).SelectMany(i => i);
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
Try:
var x = list.GroupBy(g => g.Key).Select(grp => grp.Skip(1)).SelectMany(i => i);
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
You're welcome!
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
Try:
var x = list.GroupBy(g => g.Key).Select(grp => grp.Skip(1)).SelectMany(i => i);
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
You need a small tweak to get a one-off instance like 'baz in the result:
var x = list.GroupBy(g => g.Key).Select(grp =>
grp.Count() == 1
? grp
: grp.Skip(1)).SelectMany(i => i);«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
-
You need a small tweak to get a one-off instance like 'baz in the result:
var x = list.GroupBy(g => g.Key).Select(grp =>
grp.Count() == 1
? grp
: grp.Skip(1)).SelectMany(i => i);«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
Yes, but his original question specifically excluded "baz".
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
Yes, but his original question specifically excluded "baz".
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
I tear my hair out, throw myself at your lotus feet, and beg mercy while singing [^] :omg: I'm such a sucker for a stray 'baz :wtf: Been a very weird day !
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
-
I tear my hair out, throw myself at your lotus feet, and beg mercy while singing [^] :omg: I'm such a sucker for a stray 'baz :wtf: Been a very weird day !
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
Never any need for that Bill - you are Forgiven[^] of course. (And I love Sharon's voice)
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
I tear my hair out, throw myself at your lotus feet, and beg mercy while singing [^] :omg: I'm such a sucker for a stray 'baz :wtf: Been a very weird day !
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
Please stop singing immediately. this is supposed to be a serious forum. ;P
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Please stop singing immediately. this is supposed to be a serious forum. ;P
Luc Pattyn [My Articles] Nil Volentibus Arduum
Okay, got it ... I'm switching to screaming :wtf:
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
-
Never any need for that Bill - you are Forgiven[^] of course. (And I love Sharon's voice)
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
lovely voice, reminds me a little of Sarah Brightman. thanks, Bill
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
-
Okay, got it ... I'm switching to screaming :wtf:
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
That would be fine in Q&A :cool:
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
That would be fine in Q&A :cool:
Luc Pattyn [My Articles] Nil Volentibus Arduum
Only when I have finished banging my head on the desk.
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
Only when I have finished banging my head on the desk.
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
Are you at it again, wasn't this stretch day for you? :confused:
Luc Pattyn [My Articles] Nil Volentibus Arduum