Finding multiple items in C# Generic List

No.of Views1261
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  02 Oct 2010 10:10:43
Tag : CSharp , How to
Let us say we have a list of integer and we want to find all the number greater than 100.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

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

        }
    }
}

Output 

Image Loading

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

 

Image Loading

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;elsereturn 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. 

Image Loading

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;elsereturn false; 
        }
    
    }
}

 Output 

Image Loading

that's all. hope help to all.thank you for reading.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top