LINQ 101 question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike P

    LINQ 101 question

    In this LINQ example, how do you get at the values within the groupings?

    string[] anagrams = {"from", "salt", "earn ", "last ", "near ", "form
    "};

    var orderGroups = anagrams.GroupB y(
    w =w.Trim(),
    new AnagramEquality Comparer());


    *** Sent via Developersdex http://www.developersdex.com ***
  • Jon Skeet [C# MVP]

    #2
    Re: LINQ 101 question

    On Aug 20, 4:46 pm, Mike P <mike.p...@gmai l.comwrote:
    In this LINQ example, how do you get at the values within the groupings?
    >
    string[] anagrams = {"from", "salt", "earn ", "last ", "near ", "form
    "};
    >
            var orderGroups = anagrams.GroupB y(
                        w =w.Trim(),
                        new AnagramEquality Comparer());
    GroupBy returns an IGrouping (IIRC) which has a Key property and
    implements IEnumerable<Tfo r the appropriate T - so you typically
    just use "foreach" over it.

    Here's an example:
    using System;
    using System.Collecti ons.Generic;
    using System.Linq;

    class Test
    {
    static void Main()
    {
    string[] strings = { "These", "strings", "will",
    "be", "grouped", "by", "length", "and", "then",
    "displayed" , "in", "their", "groups" };

    var groups = strings.GroupBy (x =x.Length);

    foreach (var group in groups)
    {
    Console.WriteLi ne("Words of length: {0}", group.Key);
    foreach (var word in group)
    {
    Console.WriteLi ne(" {0}", word);
    }
    }
    }
    }

    Jon

    Comment

    • vbMark

      #3
      Re: LINQ 101 question

      "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in news:05bf5f2b-589f-44db-
      be15-bb0667c74261@m4 5g2000hsb.googl egroups.com:
      On Aug 20, 4:46 pm, Mike P <mike.p...@gmai l.comwrote:
      >In this LINQ example, how do you get at the values within the
      groupings?
      >>
      >string[] anagrams = {"from", "salt", "earn ", "last ", "near ", "form
      >"};
      >>
      >        var orderGroups = anagrams.GroupB y(
      >                    w =w.Trim(),
      >                    new AnagramEquality Comparer());
      >
      GroupBy returns an IGrouping (IIRC) which has a Key property and
      implements IEnumerable<Tfo r the appropriate T - so you typically
      just use "foreach" over it.
      >
      Here's an example:
      using System;
      using System.Collecti ons.Generic;
      using System.Linq;
      >
      class Test
      {
      static void Main()
      {
      string[] strings = { "These", "strings", "will",
      "be", "grouped", "by", "length", "and", "then",
      "displayed" , "in", "their", "groups" };
      >
      var groups = strings.GroupBy (x =x.Length);
      >
      foreach (var group in groups)
      {
      Console.WriteLi ne("Words of length: {0}", group.Key);
      foreach (var word in group)
      {
      Console.WriteLi ne(" {0}", word);
      }
      }
      }
      }
      >
      Jon
      >
      What if I only want the first item and do not want to do a foreach?

      I want to do something like this:

      string myString = strings[0];

      But that is not an option.

      --
      Only the Best Freeware

      Comment

      • Peter Duniho

        #4
        Re: LINQ 101 question

        On Thu, 21 Aug 2008 12:45:38 -0700, vbMark <no@email.comwr ote:
        What if I only want the first item and do not want to do a foreach?
        >
        I want to do something like this:
        >
        string myString = strings[0];
        >
        But that is not an option.
        Try the System.Linq.Enu merable.First() method. For example, if you want
        the group of the shortest words:

        Console.WriteLi ne("The shortest words have {0} characters. They
        are:", groups.First(). Key);
        foreach (var word in groups.First())
        {
        Console.WriteLi ne(" {0}", word);
        }

        Pre-LINQ, you could of course always just put a "break" at the bottom of
        your "foreach()" loop to ensure that you only ever process the first
        element. But LINQ makes it nicer. :)

        Pete

        Comment

        Working...