SIGN UP MEMBER LOGIN:    
ARTICLE

Using Lambda Expression in FindAll()

Posted by Dhananjay Kumar Articles | .NET 4.5 October 04, 2010
In this article we will see how to use Lambda expression in FindAll().
Reader Level:

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 

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

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

List<int> lstgrthund = lst.FindAll(a=> a >100); as "a > 100" already returns true or false !

Posted by pluque the luxe Nov 16, 2011
Team Foundation Server Hosting
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
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor