Export (0) Print
Expand All

into (C# Reference)

The into contextual keyword can be used to create a temporary identifier to store the results of a group, join or select clause into a new identifier. This identifier can itself be a generator for additional query commands. When used in a group or select clause, the use of the new identifier is sometimes referred to as a continuation.

The following example shows the use of the into keyword to enable a temporary identifier fruitGroup which has an inferred type of IGrouping. By using the identifier, you can invoke the Count method on each group and select only those groups that contain two or more words.

class IntoSample1
{
    static void Main()
    {

        // Create a data source. 
        string[] words = { "apples", "blueberries", "oranges", "bananas", "apricots"};

        // Create the query. 
        var wordGroups1 =
            from w in words
            group w by w[0] into fruitGroup
            where fruitGroup.Count() >= 2
            select new { FirstLetter = fruitGroup.Key, Words = fruitGroup.Count() };

        // Execute the query. Note that we only iterate over the groups,  
        // not the items in each group 
        foreach (var item in wordGroups1)
        {
            Console.WriteLine(" {0} has {1} elements.", item.FirstLetter, item.Words);
        }

        // Keep the console window open in debug mode
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}
/* Output:
   a has 2 elements.
   b has 2 elements.
*/

The use of into in a group clause is only necessary when you want to perform additional query operations on each group. For more information, see group clause (C# Reference).

For an example of the use of into in a join clause, see join clause (C# Reference).

Community Additions

ADD
Show:
© 2014 Microsoft