Linq Example 3- Select Filtered

5:09 AM
In this example we will use filter (where in sql).

Linq Query:

//Select Filtered.
            int[] myNumbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            var evenNumbers =
                from s in myNumbers
                where s % 2 == 0
                select s;

            foreach( var t in evenNumbers)
            {
                Console.WriteLine(t.ToString());
            }


Explanation:

We have an Array of Integer containing 10 number from 1 to 10. Now i only want those number which are divisible by 2, means even numbers.

Notice the Where in above query.

Output:

2
4
6
8
10


0 comments:

Post a Comment