.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 >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

FindAll() : Find multiple items in C# List

Posted By:Dhananjay Kumar       Posted Date: October 03, 2010    Points: 50    Category: C#    URL: http://www.dotnetspark.com  

In this article I will show how to Find multiple items in C# List using FindAll method.
 

Let us say we have a list of integer and we want to find all the number greater than 100.

If list is as follow

List lst = new List();
            lst.Add(20);
            lst.Add(300);
            lst.Add(400);
            lst.Add(9);
            lst.Add(19);
            lst.Add(789);
            lst.Add(45); 

Now if we print this list

Program.cs

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

namespace ConsoleApplication24
{
    class Program
    {
        static void Main(string[] args)
        {

            List lst = new List();
            lst.Add(20);
            lst.Add(300);
            lst.Add(400);
            lst.Add(9);
            lst.Add(19);
            lst.Add(789);
            lst.Add(45); 

            foreach(var r in lst)
            {
                Console.WriteLine(r);
            }
            Console.ReadKey(true);

        }
    }
}




Now we need to find the entire element in the list greater than 100. So for this purpose we will use FindAll()



We can see that FindAll() takes a predicate.  So we need to create a predicate and that takes an integer as input parameter.
public static bool GreaterThanHun(int value)
        {
            if (value > 100)
                return true;
            else
                return false; 
        }


So when we now call this predicate as input parameter of FindAll() we will get the desired result of list numbers greater than 100.



Program.cs

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

namespace ConsoleApplication24
{
    class Program
    {
        static void Main(string[] args)
        {

            List lst = new List();
            lst.Add(20);
            lst.Add(300);
            lst.Add(400);
            lst.Add(9);
            lst.Add(19);
            lst.Add(789);
            lst.Add(45); 

            foreach(var r in lst)
            {
                Console.WriteLine(r);
            }
            Console.ReadKey(true);

            List lstgrthund = lst.FindAll(GreaterThanHun);

            

            foreach (var r in lstgrthund)
            {
                Console.WriteLine(r);

            }
            Console.ReadKey(true);
            

        }

        public static bool GreaterThanHun(int value)
        {
            if (value > 100)
                return true;
            else
                return false; 
        }
    
    }
}

Output



 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

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