LINQ to Int Array

In this example we will write LINQ queries on int array to find minimum 3 values, maximum 3 values, values between specific range, even numbers and odd numbers in the Int Array.

LINQ Query:

Below LINQ query will find the maximum three numbers from the int array.

int[] ary1 = new int[] { 10, 27, 35, 40, 50, 11, 23, 25, 39, 22, 36 }; var maxvalues = (from values in ary1 orderby (int)values descending select new { Max3Values = values }).Take(3); gvMax.DataSource = maxvalues; gvMax.DataBind();

Output:

Max3Values

50

40

39

LINQ Query:

Below LINQ query will find the numbers between 20 and 35 from the int array.

int[] ary2 = new int[] { 10, 27, 35, 40, 50, 11, 23, 25, 39, 22, 36 }; var res = from values in ary2 where (int)values >= 20 && (int)values <= 35 orderby (int)values select new { ValuesBetween20_35 = values }; gv.DataSource = res; gv.DataBind();

Output:

ValuesBetween20_35

22

23

25

27

35

LINQ Query:

Below LINQ query will find the even numbers in the int array.

int[] ary3 = new int[] { 10, 27, 35, 40, 50, 11, 23, 25, 39, 22, 36 }; var evennumbers = from values in ary3 where (int)values % 2 == 0 orderby (int)values select new { EvenNumbers = values }; gvEvenNumbers.DataSource = evennumbers; gvEvenNumbers.DataBind();

Output:

EvenNumbers

10

22

36

40

50

LINQ Query:

Below LINQ query will find the odd numbers in the int array.

int[] ary4 = new int[] { 10, 27, 35, 40, 50, 11, 23, 25, 39, 22, 36 }; var oddnumbers = from values in ary4 where (int)values % 2 == 1 orderby (int)values select new { OddNumbers = values }; gvOddNumbers.DataSource = oddnumbers; gvOddNumbers.DataBind();

Output:

OddNumbers

11

23

25

27

35

39

LINQ Query:

Below LINQ query will find minimum 3 numbers from the int array.

int[] ary5 = new int[] { 10, 27, 35, 40, 50, 11, 23, 25, 39, 22, 36 }; var minvalues = (from values in ary5 orderby (int)values ascending select new { Min3Values = values }).Take(3); gvMin.DataSource = minvalues; gvMin.DataBind();

Output:

Min3Values

10

11

22