Pages

Tuesday, December 11, 2012

Restriction Operators

Where:

Using where we can filter the records in a particular set based on some condition.

EX:

For example take one array set of numbers

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

Suppose if we want to find the elements which are less than 5 in the above set.How to achieve this using where condition in LINQ.

Declare one variable and using the where condition to above set ,store the values in variable.

var lowNums = from n in numbers where n < 5 select n;

Now lowNums is having values less than 5

code:
 public void whereSimple1()
        {
            int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

            var lowNums = from n in numbers where n < 5 select n;

            Console.WriteLine("Numbers <5:");
            foreach (var x in lowNums)
            {
                Console.WriteLine(x);
            }
            Console.ReadKey();
        }

output:

Numbers <5:

4
1
3
2
0

0 comments:

Post a Comment