.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> LINQ >> Post New Resource Bookmark and Share   

 Subscribe to Articles

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

Posted By:Dhananjay Kumar       Posted Date: October 04, 2010    Points: 50    Category: LINQ    URL: http://www.dotnetspark.com  

In this article we will find 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



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




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



Program.cs

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



Using Anonymous Method

We can use anonymous method also



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



Using Lambda

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



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



 Subscribe to Articles

     

Further Readings:

Responses
Author: Asha.MP         Company URL: http://www.dotnetspark.com
Posted Date: November 04, 2010

Hi..

This is Fine but in below program i am getting the error like parameter val is not specified near Where(IsEvenNum). How to resolve that?
Public Class LinqEX
Shared Sub main()
Dim result = From r In Enumerable.Range(1, 20).Where(IsEvenNum) Select r
For Each num In result
Console.WriteLine(num)
Next

Console.ReadLine()
End Sub
Shared Function IsEvenNum(ByVal val As Integer) As Boolean
If val Mod 2 = 0 Then
Return True
Else
Return False
End If
End Function
End Class

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend