static void Main(string[] args)
{
// The Three Parts of a LINQ Query:
// 1. Data source.
int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };
// 2. Query creation.
// numQuery is an IEnumerable<int>
var numQuery =
from num in numbers
where (num % 2) == 0
select num;
// 3. Query execution.
foreach (int num in numQuery)
{
Console.Write(“{0,1} “, num);
}
Console.WriteLine();
// LIST
List<Person> pList = new List<Person>();
pList.Add(new Person(“둘리”, 1, “123-4568-9012”, “대한민국”));
pList.Add(new Person(“희동이”, 3, “000-0000-0000”, “서울”));
pList.Add(new Person(“고길동”, 2, “000-0000-0000”, “서울”));
pList.Add(new Person(“도우너”, 5, “000-0000-0000”, “온따빠야별”));
pList.Add(new Person(“또치”, 4, “000-0000-0000”, “라스베가스”));
pList.Insert(2, new Person(“마이콜”, 6, “000-0000-0000”, “서울”));
IEnumerable<Person> seoulQuery =
from per in pList
where per.Address == “서울”
select per;
foreach (var per in seoulQuery)
{
Console.WriteLine(per.Name + “, ” + per.ID);
}
Console.WriteLine();
// query syntax
IEnumerable<Person> personQuery =
from p in pList
where (p.ID < 2) || (p.Address == “서울”)
orderby p.Name ascending
select p;
foreach (var p in personQuery)
{
Console.WriteLine(p.Name + “, ” + p.ID);
}
Console.WriteLine();
// method syntax
IEnumerable<Person> personQuery2 = pList.Where(p => (p.ID < 2) || (p.Address == “서울”)).OrderBy(p => p);
foreach (var p in personQuery2)
{
Console.WriteLine(p.Name + “, ” + p.ID);
}
Console.WriteLine();
}