33
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
5
6
public class Program
7
{
8
public static void Main()
9
{
10
string[] words =
11
{
12
"hello", "wonderful", "linq", "beautiful", "world"
13
};
14
15
// Query Syntax
16
var shortWords1 = from w in words
17
where w.Length <= 5
18
select w;
19
20
// Method Syntax
21
var shortWords2 = words.Where(x=>x.Length <= 5);
22
23
24
Console.WriteLine("ShortWords1: {0}", shortWords1.Count());
25
Console.WriteLine("ShortWords2: {0}", shortWords2.Count());
26
Console.WriteLine();
27
28
foreach(var item in shortWords2)
29
{
30
Console.WriteLine(item);
31
}
32
}
33
}
Cached Result
ShortWords1: 3
ShortWords2: 3
hello
linq
world
ShortWords2: 3
hello
linq
world