Saturday 2 June 2012

C# Tips : Querying the array using LINQ and LAMBDA Expression

You can also query the array variables using LINQ.
Here are sample example for this.
In this example there is an array of integer values which contains various number, Now we want to get numbers in array which is greater then 5.
We can do this without FOR loop.

There are two possible way to do this. One is using LINQ Query and Second is Using LAMBDA Expression


C# Example using LINQ Query :
int[] arrayNumbers = new[] { 1,2,5,9,10,1,0,9,5,6,4,3,2};
int[] selectedNumber = (from an in arrayNumbers where an > 5 select an).ToArray();

C# Example using LAMBDA Expression:
int[] arrayNumbers = new[] { 1,2,5,9,10,1,0,9,5,6,4,3,2};
int[] selectedNumberLambda = arrayNumbers.Where(an => an > 5).ToArray();

2 comments:

  1. Is Linq query also a LAMBDA expression?

    ReplyDelete
    Replies
    1. LAMBDA Expression is part of the LINQ query.

      Delete