Finding Odd numbers from a Range of Number using LINQ in C#

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

clip_image002

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

clip_image004

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

clip_image006

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;
            else
                return false; 
        }
    }
}

Output

clip_image008

Using Anonymous Method

We can use anonymous method also

clip_image010

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 in
                             Enumerable.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

clip_image011

Using Lambda

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

clip_image013

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 in
                             Enumerable.Range(1, 20).Where(num =>num%2!=0)
                                                               select r;
            foreach (var r in result)
                Console.WriteLine(r);
            Console.ReadKey(true);
        }
}
}

Output

clip_image015

Published by

Dhananjay Kumar

Dhananjay Kumar is Developer Evangelist for Infragistics. He is a 8 times Microsoft MVP and well respected Developer Advocate in India.He is the author of 900+ Blog Posts, and can often be found speaking around India at conferences and hosting free workshops for programmers across the country. So far, he has hosted 60 free workshops on various topics like JavaScript, Angular, WCF, ASP.NET MVC, C#, Azure etc. Follow him on twitter @debug_mode for all the updates about his blog posts and workshops. You can send him email at debugmode [at] outlook [dot] com

2 thoughts on “Finding Odd numbers from a Range of Number using LINQ in C#”

  1. can u please give the list of even numbers in a given range in normal console application program

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s