Using Linq to get data of a list within a list matching a search criteria


Today I am going to show how you can use LINQ to get a List of addresses from an employee list of which an employee name criteria is matched. Normally this is a tough situation to do who is new to LINQ. I am hereby sharing the code snippet on how to do the same.

I have created a class named Employee and Class named Address. Employee has got list of Addresses. We will be writing code to get the list of Addressees of an Employee whose name matches criteria. This employee has to be picked from the list of employees.

The code snippet should give you a clear understanding.

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

namespace LinqPOC {

    public class Employee {
        public string Name { get; set; }
        public List<Address> Addresses { get; set; }
    }

    public class Address {
        public string Street1 { get; set; }
        public string Street2 { get; set; }
    }

    class Program {
        static void Main(string[] args) {
           
// Insert dummy data

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

            Employee employee = new Employee();
            employee.Name = "Emp1";
            employee.Addresses = new List<Address>();
            employee.Addresses.Add(new Address { Street1 = "Emp1-Add1-Street1", Street2 = "Emp1-Add1-Street2" });
            employee.Addresses.Add(new Address { Street1 = "Emp1-Add2-Street1", Street2 = "Emp1-Add2-Street2" });
            employee.Addresses.Add(new Address { Street1 = "Emp1-Add3-Street1", Street2 = "Emp1-Add3-Street2" });
            employee.Addresses.Add(new Address { Street1 = "Emp1-Add4-Street1", Street2 = "Emp1-Add4-Street2" });
            employees.Add(employee);

            employee = new Employee();
            employee.Name = "Emp2";
            employee.Addresses = new List<Address>();
            employee.Addresses.Add(new Address { Street1 = "Emp2-Add1-Street1", Street2 = "Emp2-Add1-Street2" });
            employee.Addresses.Add(new Address { Street1 = "Emp2-Add2-Street1", Street2 = "Emp2-Add2-Street2" });
            employee.Addresses.Add(new Address { Street1 = "Emp2-Add3-Street1", Street2 = "Emp2-Add3-Street2" });
            employee.Addresses.Add(new Address { Street1 = "Emp2-Add4-Street1", Street2 = "Emp2-Add4-Street2" });
            employees.Add(employee);

            employee = new Employee();
            employee.Name = "Emp3";
            employee.Addresses = new List<Address>();
            employee.Addresses.Add(new Address { Street1 = "Emp3-Add1-Street1", Street2 = "Emp3-Add1-Street2" });
            employee.Addresses.Add(new Address { Street1 = "Emp3-Add2-Street1", Street2 = "Emp3-Add2-Street2" });
            employee.Addresses.Add(new Address { Street1 = "Emp3-Add3-Street1", Street2 = "Emp3-Add3-Street2" });
            employee.Addresses.Add(new Address { Street1 = "Emp3-Add4-Street1", Street2 = "Emp3-Add4-Street2" });
            employees.Add(employee);
 

            // Searching for the list of addressses for an employee whose name is Emp3          
            var addresses = from emp in employees
                            from add in emp.Addresses
                            where emp.Name == "Emp3"
                            select add;
            foreach (var address in addresses) {
                Console.WriteLine(address.Street1);
                Console.WriteLine(address.Street2);

            }

            Console.ReadLine();
        }
    }

}

Ebook Download
View all
Learn
View all