Output properties created by select new in CSharp

Description

The following code shows how to output properties created by select new.

Example


using System;//from   w  ww.ja  v a  2 s  .c o  m
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class MainClass {
    public static void Main() {

        string[] words = { "b", "c", "a", "ba", "ae", "ch" };

        var wordGroups =
            from w in words
            group w by w[0] into g
            select new { FirstLetter = g.Key, Words = g };

        foreach (var g in wordGroups) {
            Console.WriteLine("Words that start with the letter '{0}':", g.FirstLetter);
            foreach (var w in g.Words) {
                Console.WriteLine(w);
            }
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    LINQ »




Operator
Select
Where
OrderBy
Group
Join
Let
LINQ