using System;
using System.Linq;
/*
* Query using C# and doing it again using LINQ
* Deferrred LINQ query Execution
*/
namespace Console_LINQ_DeferredQueryExecution
{
class Program
{
static void Main(string[] args)
{
int[] numbers = { 1, 2, 3, 4, 6, 7, 9, 5, 8 };
//find all even numbers using C#, before LINQ was invented
Console.WriteLine("\n===========C# to Objects==========\n");
for (int i = 0; i < numbers.Length; i++)
{
if (numbers[i] % 2 == 0)
{
Console.WriteLine(numbers[i]);
}
}
Console.WriteLine("\n===========LINQ to Objects==========\n");
int[] numbers2 = { 1, 2, 3, 4, 6, 7, 9, 5, 8 };
//LINQ SQL based syntax
var query = from number in numbers2
where number % 2 == 0
select number;
//Method syntax
//var query = numbers.Where(number => number % 2 == 0);
foreach (var item in query)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}
OUTPUT
===========C# to Objects==========
2
4
6
8
===========LINQ to Objects==========
2
4
6
8
No comments:
Post a Comment