Execute LINQ Query More Than Once Time


using System;
using System.Linq;


class RerunLinq
{
static void Main()
{

//WITHOUT HELP OF LINQ

Console.WriteLine("Without the help of linq");
int[] arr = {1,2,3,4,5,6,7,8,9,10};
int counteven = 0;

foreach(int i in arr)
{
if(i%2  == 0)
counteven++;
}
Console.WriteLine("Total Number of even is " + counteven);

arr[0] = 2;
counteven =0;

foreach(int i in arr)
{
if(i%2  == 0)
counteven++;
}
Console.WriteLine("Total Number of even is " + counteven);
Console.WriteLine("========================================");


// WITH THE HELP OF LINQ 

Console.WriteLine("With the help of linq");

int[] arr1 = {1,2,3,4,5,6,7,8,9,10};
var result = from a in arr1
    where (a%2 == 0)
    select a;

int counteven1 = result.ToArray().Length;
Console.WriteLine("Total Number of even is " + counteven1);
arr1[0] = 2;
counteven1 = result.ToArray().Length;
Console.WriteLine("Total Number of even is " + counteven1);

}
}


OUTPUT







Execute LINQ Query More Than Once Time Execute LINQ Query More Than Once Time Reviewed by Baljeet Singh on 11:57 Rating: 5

No comments:

Powered by Blogger.