Using Lambda expression in FindAll()

I got a critical comment from @amazedsaint. You can read his blog http://amazedsaint.blogspot.com/

If you read my previous post http://dhananjaykumar.net/2010/10/01/findall-finding-multiple-items-in-c-list/ ,

I am using a method GreaterThanHun As predicate to pass as parameter to FindAll()

Instead of that we can use

1. Anonymous method

2. Lambda expression

So, I am going to show you here how we could use Lambda expression

clip_image002

So here we are just writing a lambda instead of calling a function.

Program.cs

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication24
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> lst = new List<int>();
            lst.Add(20);
            lst.Add(300);
            lst.Add(400);
            lst.Add(9);
            lst.Add(19);
            lst.Add(789);
            lst.Add(45);
            List<int> lstgrthund = lst.FindAll(a=>a>100? true:false);
            foreach (var r in lstgrthund)
            {
                Console.WriteLine(r);
            }
            Console.ReadKey(true);
        }

Output

clip_image004

Dhananjay Kumar is Developer, Blogger , Speaker, Learner , Mindcracker & Microsoft MVP.

Posted in CSharp, Miscellaneous
3 comments on “Using Lambda expression in FindAll()
  1. Anonymous says:

    i think
    List lstgrthund = lst.FindAll(a>100);
    would have done the trick… since its a predicate

  2. Dhananjay Kumar says:

    Yes my purpose was just to explain in rather more detail to beginner

  3. Patrick says:

    Dhananjay… i didn’t want to register on dotnet spark, but I wanted to send you a comment on your article “Step by Step tutorial of REST Enabled Service in WCF”

    Nicely done and relevant. There was, however a small bug in your example code. You are missing a bracket after the using in the following code:

    public static string AddWithoutParameter()
    {
    using (HttpResponseMessage response = new HttpClient().Get(uri))

    int res = response.Content.ReadAsDataContract();
    return res.ToString();
    }
    }

    Thanks again for the nice article!
    Patrick

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 )

Twitter picture

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

Facebook photo

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

Connecting to %s

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Categories
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my current or previous employer's view in anyway. © Copyright 2013
Follow

Get every new post delivered to your Inbox.

Join 2,134 other followers

%d bloggers like this: