Please consider the following code :
string[] words =
{ "Too", "much", "of", "anything", "is" ,"good","for","nothing"};
var groups =from word in words
orderby word ascending
group word by word.Length into lengthGroups
orderby lengthGroups.Key descending
select new {Length=lengthGroups.Key, Words=lengthGroups};
foreach (var group in groups)
{
Console.WriteLine("Words of length " + group.Length);
foreach (string word in group.Words)
Console.WriteLine(" " + word);
}
Why do we need the keyword “new” here?.can you give some other simple example to understand it properly?
The
newkeyword is used to create an instance of a class.In this case it’s an anonymous class. You can use the same syntax to create an anonmyous object outside the scope of a LINQ query: