How to Find Odd numbers from a Range of Number using LINQ in C#

No.of Views1457
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  12 Oct 2010 08:10:46
Tag : CSharp , Miscellaneous
we have a range of number from 1 to 20 and we want to find odd among them using LINQ .
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

If we have a range of number from 1 to 20 and we want to find odd among them using LINQ .
We have three options for that

1.    Calling a Predicate
2.    Anonymous Method
3.    Lambda 

Image Loading

So here we can see that we need to pass a predicate to evaluate the condition.

Calling a Predicate

Function we will pass as predicate is as below

Image Loading

We will pass this function as predicate to find odd numbers from the range of numbers. 

Image Loading

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication25
{class Program
    {static void Main(string[] args)
        {
            var result = from r in 
                         Enumerable.Range(1, 20).Where(GetOdd)
                         select r;foreach (var r in result)
                Console.WriteLine(r);
            Console.ReadKey(true);
        }static bool GetOdd(int number)
        {if (number % 2 != 0)return true;elsereturn false; 
        }
    }
}

Output 

Image Loading

Using Anonymous Method

We can use anonymous method also

Image Loading

Anonymous method is taking an input parameter and returning a Boolean value.

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication25
{class Program
    {static void Main(string[] args)
        {
            var result = from r inEnumerable.Range(1, 20).Where(delegate(int number)
                                                             {if (number % 2 != 0)return true;return false; })
                                                               select r;foreach (var r in result)
                Console.WriteLine(r);
            Console.ReadKey(true);
        }
}
}

 Output

Image Loading

Using Lambda

To make our query more readable in version 3.0 and onwards we can use Lambda instead of anonymous method also. 

Image Loading

Lambda is very simple returning true when reminder is 0.
Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication25
{class Program
    {static void Main(string[] args)
        {
            var result = from r inEnumerable.Range(1, 20).Where(num =>num%2!=0)
                                                               select r;foreach (var r in result)
                Console.WriteLine(r);
            Console.ReadKey(true);
        }
}
}

Output  

Image Loading

Conclusion

Once you read this article, now you able to find the odd numbers easilly in C#.happy coding.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top