MGZ NetArt

Software Development and Support

Home
Design Patterns
C# Tips
AsyncCallback
Asynchronous Form.ShowDia
BackgroundWorker
Connection String
CopyTo() vs Clone()
Convert Color To String
Dispose vs Finalize
Debug mode
Delete Duplicate in SQL
Drag&Drop for TreeView
Edit Full Path Property
Hiding And Overriding
Polimorphism
How to use Resources
Run Time DLL
Simple Threading
Singleton Connection
Save Win Form as JPG File
Save Object in Registry
Soap Serialization
Sort and CompareTo
Working With List<>
Examples & Videos
MRDS Examples
Resume
Contact Us
Exists, Find, FindAll and FindIndex in List<> Object and elegant Lambda code

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Linq.Expressions;

namespace ConsoleApplicationTest

{

    class Program

    {

        static void Main(string[] args)

        {

            List<Employee> _employeeList = new List<Employee>();

           

            Employee e1 = new Employee("Mike", "Dorothy", "111-22-3344");

            Employee e2 = new Employee("Dorothy", "Mike", "111-22-4455");

            Employee e3 = new Employee("Mike", "Dorothy", "111-22-3344");

            Employee e4 = new Employee("Dorothy", "Mike", "111-22-4455");

           

            _employeeList.Add(e1);

            _employeeList.Add(e2);

            _employeeList.Add(e3);

            _employeeList.Add(e4);

            

            //Exists=======================================

            bool ifExist = _employeeList.Exists(delegate(Employee e) { return e.FirstName == "Dorothy"; });

            Console.WriteLine(ifExist.ToString());

            //same idea implemented with Lambdas Expression

            bool ifExist1 = _employeeList.Exists(e => e.FirstName == "Dorothy");

            Console.WriteLine(ifExist1.ToString());


            //Find==========================================

            Employee test = _employeeList.Find(delegate(Employee e) { return e.FirstName == "Mike"; });

            if(test!=null)

                Console.WriteLine(test.SSI);

            Employee test1 = _employeeList.Find(delegate(Employee e) { return e == e1; });

            if (test1 != null)

                Console.WriteLine(test1.SSI);

            //same thing implemented with Lambdas Expression

            Employee test2 = _employeeList.Find(e => e.FirstName == "Dorothy");

            if(test2!=null)

                Console.WriteLine(test2.SSI);

            //same thing implemented with Lambdas Expression

            Employee test3 = _employeeList.Find(e => e == e1);

            if (test3 != null)

                Console.WriteLine(test3.SSI);


            //FindAll========================================

            List<Employee> testlist = _employeeList.FindAll(delegate(Employee e) { return e.FirstName == "Dorothy"; });

            if (testlist != null)

                foreach (Employee emp in testlist)

                {

                    Console.WriteLine(emp.SSI);

                }

            //same logic implemented with Lambdas Expression

            List<Employee> testlist1 = _employeeList.FindAll(e => e.FirstName == "Mike");

            if(testlist1!=null)

                foreach (Employee emp in testlist1)

                {

                    Console.WriteLine(emp.SSI);

                }


            //FindIndex=======================================

            int i = _employeeList.FindIndex(delegate(Employee e) { return e.FirstName == "Dorothy"; });

            Console.WriteLine(i.ToString());

            //same logic implemented with Lambdas Expression

            int i1 = _employeeList.FindIndex(e => e.FirstName == "Dorothy");

            Console.WriteLine(i1.ToString());

        }

    }


    class Employee

    {

        public Employee(string firstname, string lastname, string ssi)

        {

            FirstName = firstname;

            LastName = lastname;

            SSI = ssi;

        }

        public string FirstName;

        public string LastName;

        public string SSI;

    }

}



Additional Resources: bytes.com, dotnetperls.com, microsoft, beware of ListFind, Eric White Blog