LINQ to Lists / Collections

LINQ to Lists/collections means writing LINQ queries on a list or collection. Using LINQ queries on collection or list, we can filter, sort, or remove duplicate elements with minimal coding.

Syntax of LINQ to Lists or Collections

Following is the syntax of writing LINQ queries on a list or collection to get the required elements.

 

C# Code

 

var result = from e in objEmp
             select new
             {
               Name = e.Name,
               Location = e.Location
             };

VB.NET Code

 

Dim result = From e In objEmp Select New With {.Name = e.Name, .Location = e.Location}

If you observe the above syntax, we wrote the LINQ query to get the required data from the “objEmp” collection/list object.

Example of LINQ to Lists / Collections

Following is the example of LINQ to Lists or collection to get elements from the collection where employee location equals “Chennai”.

 

C# Code

 

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

namespace Linqtutorials
{
  class Program
  {
    static void Main(string[] args)
    {
      List<Employee> objEmp = new List<Employee>()
      {
        new Employee { EmpId=1,Name = "Suresh Dasari", Location="Chennai" },
        new Employee { EmpId=2,Name = "Rohini Alavala", Location="Chennai" },
        new Employee { EmpId=3,Name = "Praveen Alavala", Location="Guntur" },
        new Employee { EmpId=4,Name = "Sateesh Alavala", Location ="Vizag"},
      };
      var result = from e in objEmp where e.Location.Equals("Chennai")
                   select new
                   {
                     Name = e.Name,
                     Location = e.Location
                   };
      foreach (var item in result)
      {
        Console.WriteLine(item.Name + "\t | " + item.Location);
      }
      Console.ReadLine();
    }
  }
  class Employee
  {
    public int EmpId { get; set; }
    public string Name { get; set; }
    public string Location { get; set; }
  }
}

VB.NET Code

 

Module Module1

Sub Main()
Dim objEmp As New List(Of Employee)() From {
New Employee With {.EmpId = 1, .Name = "Suresh Dasari", .Location = "Chennai"},
New Employee With {.EmpId = 2, .Name = "Rohini Alavala", .Location = "Chennai"},
New Employee With {.EmpId = 3, .Name = "Praveen Alavala", .Location = "Guntur"},
New Employee With {.EmpId = 4, .Name = "Sateesh Alavala", .Location = "Vizag"}
}
Dim result = From e In objEmp Where e.Location.Equals("Chennai")
Select New With {.Name = e.Name, .Location = e.Location}
For Each item In result
Console.WriteLine(item.Name + vbTab & " | " + item.Location)
Next
Console.ReadLine()
End Sub
Class Employee
Public Property EmpId() As Int32
Get
Return m_EmpId
End Get
Set(ByVal value As Int32)
m_EmpId = value
End Set
End Property
Private m_EmpId As Int32
Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property
Private m_Name As String
Public Property Location() As String
Get
Return m_Location
End Get
Set(ByVal value As String)
m_Location = value
End Set
End Property
Private m_Location As String
End Class
End Module

If you observe the above code, we used the LINQ query on the list “objEmp” to get the required elements based on the requirement.

Result of LINQ to Lists Example

Following is the result of the LINQ to Lists Example.

 

Suresh Dasari    | Chennai
Rohini Alavala   | Chennai

This is how we can use LINQ queries with lists or collections to get the required data in c#, vb.net.