Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. LINQ: Remove first entry of groups in a list

LINQ: Remove first entry of groups in a list

Scheduled Pinned Locked Moved C#
csharplinqtutorialquestion
14 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Mc_Topaz

    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

    OriginalGriffO Offline
    OriginalGriffO Offline
    OriginalGriff
    wrote on last edited by
    #2

    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!

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

    M B 2 Replies Last reply
    0
    • OriginalGriffO OriginalGriff

      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!

      M Offline
      M Offline
      Mc_Topaz
      wrote on last edited by
      #3

      Thank you VERY much! :)

      OriginalGriffO 1 Reply Last reply
      0
      • M Mc_Topaz

        Thank you VERY much! :)

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #4

        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!

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          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!

          B Offline
          B Offline
          BillWoodruff
          wrote on last edited by
          #5

          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

          OriginalGriffO 1 Reply Last reply
          0
          • B BillWoodruff

            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

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #6

            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 have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            B 1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              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!

              B Offline
              B Offline
              BillWoodruff
              wrote on last edited by
              #7

              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

              OriginalGriffO L 2 Replies Last reply
              0
              • B BillWoodruff

                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

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #8

                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 have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                B 1 Reply Last reply
                0
                • B BillWoodruff

                  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

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #9

                  Please stop singing immediately. this is supposed to be a serious forum. ;P

                  Luc Pattyn [My Articles] Nil Volentibus Arduum

                  B 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    Please stop singing immediately. this is supposed to be a serious forum. ;P

                    Luc Pattyn [My Articles] Nil Volentibus Arduum

                    B Offline
                    B Offline
                    BillWoodruff
                    wrote on last edited by
                    #10

                    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

                    L 1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      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!

                      B Offline
                      B Offline
                      BillWoodruff
                      wrote on last edited by
                      #11

                      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

                      1 Reply Last reply
                      0
                      • B BillWoodruff

                        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

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #12

                        That would be fine in Q&A :cool:

                        Luc Pattyn [My Articles] Nil Volentibus Arduum

                        OriginalGriffO 1 Reply Last reply
                        0
                        • L Luc Pattyn

                          That would be fine in Q&A :cool:

                          Luc Pattyn [My Articles] Nil Volentibus Arduum

                          OriginalGriffO Offline
                          OriginalGriffO Offline
                          OriginalGriff
                          wrote on last edited by
                          #13

                          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!

                          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                          L 1 Reply Last reply
                          0
                          • OriginalGriffO OriginalGriff

                            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!

                            L Offline
                            L Offline
                            Luc Pattyn
                            wrote on last edited by
                            #14

                            Are you at it again, wasn't this stretch day for you? :confused:

                            Luc Pattyn [My Articles] Nil Volentibus Arduum

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups