Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
BRH 58
#ASP.NET 55
ASP.NET 50
#DOTNET 49
.NET 40
WCF 21
DOTNET 12
c# 8
windows azure 7
SILVERLIGHT 7

Archive · View All
April 2011 9
March 2011 9
February 2011 8
December 2010 7
November 2010 5
September 2010 5
August 2010 5
May 2011 4
October 2010 4
January 2011 2

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

Nov 19 2010 8:19AM by Dhananjay Kumar   

This article will show you , how could we use Predicate , Anonymous method and Lambdas with LINQ to find out all odd numbers from a range of number.

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

staticvoid 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);
}

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

var result = from r in
             Enumerable.Range(1, 20).Where(GetOdd)
             select r;

Program.cs

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

namespace ConsoleApplication25
{
    classProgram
    {
        staticvoid 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);
        }
        staticboolGetOdd(int number)
        {
            if (number % 2 != 0)
                return true;
            else
                return false; 
        }
    }
}

Output

clip_image008

Using Anonymous Method

We can use anonymous method also

var result = from r in
Enumerable.Range(1, 20).Where(delegate(int number)
                                { 
                                    if (number % 2 != 0)
                                        return true;
                                        return false; })
                                select r;

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

Program.cs

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

namespace ConsoleApplication25
{
    classProgram
    {
        staticvoid 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_image008[1]

Using Lambda

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

var result = from r in
                         Enumerable.Range(1, 20).Where(num =>num%2!=0)
                                                           select r;

Lambda is very simple returning true when reminder is 0.

Program.cs

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

namespace ConsoleApplication25
{
    classProgram
    {
        staticvoid 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_image014

Tags: #DOTNET, LINQ, DOTNET, #ASP.NET, c#, BRH,


Dhananjay Kumar
49 · 4% · 1198
0
Liked
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]