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. LINQ
  4. Frequency Count In LINQ

Frequency Count In LINQ

Scheduled Pinned Locked Moved LINQ
csharplinqquestion
7 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 Offline
    M Offline
    mark_w_
    wrote on last edited by
    #1

    Good Afternoon. I have a list called colours I have the following LINQ statement that returns that list var foo = from s in colours select s ; What I need to do is also select the frequency, so say the list contained red,blue,red,yellow,yellow,green,green,yellow I would get returned red 2 blue 1 yellow 3 green 2 Is this possible using LINQ? any advise, links keywords most welcome Regards Mark

    M 1 Reply Last reply
    0
    • M mark_w_

      Good Afternoon. I have a list called colours I have the following LINQ statement that returns that list var foo = from s in colours select s ; What I need to do is also select the frequency, so say the list contained red,blue,red,yellow,yellow,green,green,yellow I would get returned red 2 blue 1 yellow 3 green 2 Is this possible using LINQ? any advise, links keywords most welcome Regards Mark

      M Offline
      M Offline
      mark_w_
      wrote on last edited by
      #2

      I have now got this, which works, but i need the grouping to be case insensitive var foo = from s in colours group colours by s into ss select new {colour= ss.Key,score=ss.Count()} any help most welcome

      G 1 Reply Last reply
      0
      • M mark_w_

        I have now got this, which works, but i need the grouping to be case insensitive var foo = from s in colours group colours by s into ss select new {colour= ss.Key,score=ss.Count()} any help most welcome

        G Offline
        G Offline
        Gideon Engelberth
        wrote on last edited by
        #3

        Assuming that s is type string, you should be able to say group colours by s.ToLower()

        M 1 Reply Last reply
        0
        • G Gideon Engelberth

          Assuming that s is type string, you should be able to say group colours by s.ToLower()

          M Offline
          M Offline
          mark_w_
          wrote on last edited by
          #4

          Yes s is a string, I have tried that, but it means that my results are in lower case :( Thanks

          E 1 Reply Last reply
          0
          • M mark_w_

            Yes s is a string, I have tried that, but it means that my results are in lower case :( Thanks

            E Offline
            E Offline
            Eslam Afifi
            wrote on last edited by
            #5

            This gives you the first element in the group as the key which I think is what you want. If not, you can change the projection lambda or use another overload of the method.

            var foo = colors.GroupBy(color => color,
            (key, elements) => new { Color = key, Count = elements.Count() }, // projection
            new CaseInsensitiveEqualityComparer());

            class CaseInsensitiveEqualityComparer : IEqualityComparer<string>
            {
            #region IEqualityComparer<string> Members

            public bool Equals(string x, string y)
            {
                return x.ToLower() == y.ToLower();
            }
            
            public int GetHashCode(string obj)
            {
                return obj.ToLower().GetHashCode();
            }
            
            #endregion
            

            }

            Eslam Afifi

            D 1 Reply Last reply
            0
            • E Eslam Afifi

              This gives you the first element in the group as the key which I think is what you want. If not, you can change the projection lambda or use another overload of the method.

              var foo = colors.GroupBy(color => color,
              (key, elements) => new { Color = key, Count = elements.Count() }, // projection
              new CaseInsensitiveEqualityComparer());

              class CaseInsensitiveEqualityComparer : IEqualityComparer<string>
              {
              #region IEqualityComparer<string> Members

              public bool Equals(string x, string y)
              {
                  return x.ToLower() == y.ToLower();
              }
              
              public int GetHashCode(string obj)
              {
                  return obj.ToLower().GetHashCode();
              }
              
              #endregion
              

              }

              Eslam Afifi

              D Offline
              D Offline
              Daniel Grunwald
              wrote on last edited by
              #6

              You don't have to write your own CaseInsensitiveEqualityComparer, there's StringComparer.CurrentCultureIgnoreCase.

              E 1 Reply Last reply
              0
              • D Daniel Grunwald

                You don't have to write your own CaseInsensitiveEqualityComparer, there's StringComparer.CurrentCultureIgnoreCase.

                E Offline
                E Offline
                Eslam Afifi
                wrote on last edited by
                #7

                I remembered such class exists. When I was answering the question I thought it was System.Collections.CaseInsensitiveComparer but I checked and it isn't an IEqualityComparer. Gideon Engelberth suggested ToLower so I decided to go with it at least to illustrate the IEqualityComparer interface. Thanks for reminding me where to find it :) I'll try to never forget it again, it is an IComparer too :cool:

                Eslam Afifi

                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