using System;
using System.Linq;
static class HelloWorld
{
static void Main()
{
string[] words = { "hello", "wonderful", "linq", "beautiful", "world" };
var shortWords = from word in words where word.Length <= 5
select word;
foreach (var word in shortWords)
Console.WriteLine(word);
}
}
Old School Version of above hello world,
using System;
static class HelloWorld
{
static void Main()
{
string[] words = new string[] {
"hello", "wonderful", "linq", "beautiful", "world" };
foreach (string word in words)
{
if (word.Length <= 5)
Console.WriteLine(word);
}
}
}
Why LINQ?
As the requirement could be more complicated , as below,
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};
Comments
Post a Comment