How to Find an Item in List<T>

In this tutorial, we are going to learn how to find a item in List<T>.

We will use Find extension method to find an item in a list.

Find(Predicate<T>)

For example, we initialize a list as below.

List<string> list = new List<string> { "Tomato", "Potato", "Mango", "Orange", "Grapefruit", "Grape", "Melon" };

In above example, T is string data type (List<string>).

Now we want to find “Potato” in above list, so we can write as below.

var find = list.Find(x => x == "Potato");

If there is no “Potato” in the list, the result of Find extension method will return null.

Here is a full example code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProgrammingTips
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> list = new List<string> { "Tomato", "Potato", "Mango", "Orange", "Grapefruit", "Grape", "Melon" };
            var find = list.Find(x => x == "Potato");
            Console.WriteLine(find);
        }
    }
}

Next,  we want to find an item in a list of employee. We create an employee class with EmployeeName and EmployeeID properties.

In the example below, T is Employee (List<Employee>)

public class Employee
{
   public string EmployeeName { get; set; }
   public string EmployeeID { get; set; }
}

Then we initialize a list of employees as below.

List<Employee> list = new List<Employee>
{
                new Employee
                {
                    EmployeeID = "Emp0001", EmployeeName = "James"
                }, 
                new Employee
                {
                    EmployeeID = "Emp0002", EmployeeName = "Jonh",
                },
                new Employee
                {
                    EmployeeID = "Emp0003", EmployeeName = "William"
                }
            };
            

Now we want to find an employee that employee name is “Jonh“;
We can write as below.

var find = list.Find(x => x.EmployeeName == “Jonh“)

Here is a full example code.

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

namespace ProgrammingTips
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Employee> list = new List<Employee>
            {
                new Employee
                {
                    EmployeeID = "Emp0001", EmployeeName = "James"
                }, 
                new Employee
                {
                    EmployeeID = "Emp0002", EmployeeName = "Jonh",
                },
                new Employee
                {
                    EmployeeID = "Emp0003", EmployeeName = "William"
                }
            };
            var find = list.Find(x => x.EmployeeName == "Jonh");
            Console.WriteLine(find);
        }
    }
    public class Employee
    {
        public string EmployeeName { get; set; }
        public string EmployeeID { get; set; }
    }
}