SIGN UP MEMBER LOGIN:    
ARTICLE

Finding Odd numbers using LINQ in C#

Posted by Dhananjay Kumar Articles | LINQ with C# October 05, 2010
In this article we will see how to find odd numbers from a range of number using LINQ in C#.
Reader Level:

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 
1.gif

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 

2.gif 

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

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

4.gif

Using Anonymous Method

We can use anonymous method also 

5.gif 

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 

4.gif 

Using Lambda 

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

7.gif 

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 

4.gif
 

erver'>
Login to add your contents and source code to this article
share this article :
post comment
 

a simple, basic and easy to grasp example...thanks :)

Posted by jag singh Oct 08, 2010
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Nevron Gauge for SharePoint
Become a Sponsor