Linq Example 9 : Skip Nested

3:52 AM
Linq Query:

//Skip Nested
            int[] myNumbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            var oddNumbers =
             (   from n in myNumbers
                let IsOdd = (bool)(n % 2 != 0)
                let IsEven = !IsOdd
                select new { n, IsOdd, IsEven }).Skip(2); //Skipping First Two [0,1 will be skipped]


            foreach (var t in oddNumbers)
            {
                Console.WriteLine("number : {0} , IsOdd : {1}, IsEven : {2}", t.n, t.IsOdd, t.IsEven);
            }



Output: 


number : 2 , IsOdd : False, IsEven : True
number : 3 , IsOdd : True, IsEven : False
number : 4 , IsOdd : False, IsEven : True
number : 5 , IsOdd : True, IsEven : False
number : 6 , IsOdd : False, IsEven : True
number : 7 , IsOdd : True, IsEven : False
number : 8 , IsOdd : False, IsEven : True
number : 9 , IsOdd : True, IsEven : False
number : 10 , IsOdd : False, IsEven : True
 Here 0,1 will not be added to "oddNumbers" list as we are skipping the First Two.

0 comments:

Post a Comment