SIGN UP MEMBER LOGIN:    
ARTICLE

Finding multiple items in C# List

Posted by Dhananjay Kumar Articles | C# Language October 04, 2010
In this article we will see how to find multiple items in C# list.
Reader Level:

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

If list is as follows 

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

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<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);
            foreach(var r in lst)
            {
                Console.WriteLine(r);
            }
            Console.ReadKey(true);
        }
    }
}

1.gif

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

2.gif
 
We can see that FindAll() takes a predicate.  So we need to create a predicate 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 the input parameter of FindAll() we will get the desired result of list numbers greater than 100. 

List<int> lstgrthund = lst.FindAll(GreaterThanHun);

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);
            foreach(var r in lst)
            {
                Console.WriteLine(r);
            }
            Console.ReadKey(true);
            List<int> 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 
 
3.gif

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

must share how to use rich textbox i need

Posted by Hussain Munaf Mar 24, 2011

Thankyu for sharing that nice article.....

Posted by Manish Tewatia Jan 31, 2011

Yeah.  Good alternate the LinQ way.

Posted by Sivaraman Dhamodaran Oct 26, 2010

Thanks for the sharing the Nice Feature on the Generic List collection class.

Posted by Sivaraman Dhamodaran Oct 26, 2010

I think look much better and more compact.
lst.FindAll(o => o > 100);

Posted by Alexander Oct 07, 2010
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.
Nevron Gauge for SharePoint
Become a Sponsor