LINQ to Lists / Collections

  Total Views : 132
  Comments : 0
 
  Keywords : How to use linq queries with lists or collections in c# / vb.net with example, Query list / collection objects in linq with example in c# / vb.net, Use linq to collections in c# / vb.net

Here we will learn LINQ to Lists or collections in c#, vb.net with example and how to write LINQ queries to get elements from list or collection in c#, vb.net with example.

LINQ to Lists / Collections

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

Syntax of LINQ to Lists or Collections

Following is the syntax of writing LINQ queries on list or collection to get 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 above syntax we written LINQ query to get required data from from “objEmp” collection / list object.

Example of LINQ to Lists / Collections

Following is the example of LINQ to Lists or collection to get elements from collection where employee location equals to “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 above code, we used LINQ query on list “objEmp” to get required elements based on requirement.

Output of LINQ to Lists Example

Following is the result of LINQ to Lists Example

 

Suresh Dasari    | Chennai

Rohini Alavala   | Chennai

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

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User