LINQ ToLookup() Method

  Total Views : 263
  Comments : 0
 
  Keywords : Linq tolookup operator in c# / vb.net with example, How to use linq tolookup method in c# / vb.net with example, Linq tolookup method to return key value pair values from source list

Here we will learn LINQ ToLookup method with example in c#, vb.net and how to use LINQ ToLookup() method to extract a set of key / value pair from input list with example in query syntax and method syntax using c#, vb.net.

LINQ ToLookup Operator

In LINQ ToLookup operator is an extension method and it is used to extracts a set of Key / value pair from the source. Here each element in the resultant collection is a generic Lookup object, which holds the key and a sub sequence items that matched the key.

Syntax of LINQ ToLookup Operator

Following is the syntax of using LINQ ToLookup operator to return collection as key / value pair.

 

C# Code

 

var emp = objEmployee.ToLookup(x => x.Department);

VB.NET Code

 

Dim emp = objEmployee.ToLookup(Function(x) x.Department)

If you observe above syntax we are converting “objEmployee” collection to key / value pair list using ToLookup() operator.

LINQ ToLookup() Operator in Method Syntax Example

Following is the example of using LINQ ToLookup() in method syntax to convert input collection items as key / value pair list.

 

C# Code

 

using System;

using System.Collections.Generic;

using System.Linq;

 

namespace Linqtutorials

{

class Program

{

staticvoid Main(string[] args)

{

List<Employee> objEmployee = new List<Employee>()

{

new Employee(){ Name="Ashish Sharma", Department="Marketing", Country="India"},

new Employee(){ Name="John Smith", Department="IT", Country="Australia"},

new Employee(){ Name="Kim Jong", Department="Sales", Country="China"},

new Employee(){ Name="Marcia Adams", Department="HR", Country="USA"},

new Employee(){ Name="John Doe", Department="Operations", Country="Canada"}

};

var emp = objEmployee.ToLookup(x => x.Department);

 

Console.WriteLine("Grouping Employees by Department");

Console.WriteLine("---------------------------------");

 

foreach (var KeyValurPair in emp)

{

Console.WriteLine(KeyValurPair.Key);

 

// Lookup employees by Department

 

foreach (var item in emp[KeyValurPair.Key])

{

Console.WriteLine("\t" + item.Name + "\t" + item.Department + "\t" + item.Country);

}

}

Console.ReadLine();

}

}

class Employee

{

public string Name { get; set; }

public string Department { get; set; }

public string Country { get; set; }

}

}

VB.NET Code

 

Module Module1

 

Sub Main()

Dim objEmployee As New List(Of Employee)() From {

New Employee() With {.Name = "Ashish Sharma", .Department = "Marketing", .Country = "India"},

New Employee() With {.Name = "John Smith", .Department = "IT", .Country = "Australia"},

New Employee() With {.Name = "Kim Jong", .Department = "Sales", .Country = "China"},

New Employee() With {.Name = "Marcia Adams", .Department = "HR", .Country = "USA"},

New Employee() With {.Name = "John Doe", .Department = "Operations", .Country = "Canada"}

}

Dim emp = objEmployee.ToLookup(Function(x) x.Department)

Console.WriteLine("Grouping Employees by Department")

Console.WriteLine("---------------------------------")

For Each KeyValurPair In emp

Console.WriteLine(KeyValurPair.Key)

' Lookup employees by Department

For Each item In emp(KeyValurPair.Key)

Console.WriteLine(vbTab + item.Name + vbTab + item.Department + vbTab + item.Country)

Next

Next

Console.ReadLine()

End Sub

Class Employee

Public Property Name() As String

Get

Return m_Name

End Get

Set(value As String)

m_Name = Value

End Set

End Property

Private m_Name As String

Public Property Department() As String

Get

Return m_Department

End Get

Set(value As String)

m_Department = Value

End Set

End Property

Private m_Department As String

Public Property Country() As String

Get

Return m_Country

End Get

Set(value As String)

m_Country = Value

End Set

End Property

Private m_Country As String

End Class

End Module

In above example we grouped Employees by Department using ToLookup method. Since ToLookup produces key / value pair so we used it in the foreach loop and the inner loop extracts the values based on the Key passed as input.

Output LINQ ToLookup() Operator Example

Following is result of LINQ ToLookup() operator example to convert collection to key / value pair collection.

 

Output of LINQ ToLookup() Operator Example in Method Syntax

LINQ ToLookup in Query Syntax Example

Following is the example of using ToLookup in query syntax

 

C# Code

 

using System;

using System.Collections.Generic;

using System.Linq;

 

namespace Linqtutorials

{

class Program

{

staticvoid Main(string[] args)

{

List<Employee> objEmployee = new List<Employee>()

{

new Employee(){ Name="Ashish Sharma", Department="Marketing", Country="India"},

new Employee(){ Name="John Smith", Department="IT", Country="Australia"},

new Employee(){ Name="Kim Jong", Department="Sales", Country="China"},

new Employee(){ Name="Marcia Adams", Department="HR", Country="USA"},

new Employee(){ Name="John Doe", Department="Operations", Country="Canada"}

};

var emp = (from employee in objEmployee

select employee).ToLookup(x => x.Department);

 

Console.WriteLine("Grouping Employees by Department");

Console.WriteLine("---------------------------------");

 

foreach (var KeyValurPair in emp)

{

Console.WriteLine(KeyValurPair.Key);

 

// Lookup employees by Department

 

foreach (var item in emp[KeyValurPair.Key])

{

Console.WriteLine("\t" + item.Name + "\t" + item.Department + "\t" + item.Country);

}

}

Console.ReadLine();

 

}

}

class Employee

{

public string Name { get; set; }

public string Department { get; set; }

public string Country { get; set; }

}

}

VB.NET Code

 

Module Module1

 

Sub Main()

Dim objEmployee As New List(Of Employee)() From {

New Employee() With {.Name = "Ashish Sharma", .Department = "Marketing", .Country = "India"},

New Employee() With {.Name = "John Smith", .Department = "IT", .Country = "Australia"},

New Employee() With {.Name = "Kim Jong", .Department = "Sales", .Country = "China"},

New Employee() With {.Name = "Marcia Adams", .Department = "HR", .Country = "USA"},

New Employee() With {.Name = "John Doe", .Department = "Operations", .Country = "Canada"}

}

Dim emp = (From employee In objEmployee).ToLookup(Function(x) x.Department)

Console.WriteLine("Grouping Employees by Department")

Console.WriteLine("---------------------------------")

For Each KeyValurPair In emp

Console.WriteLine(KeyValurPair.Key)

' Lookup employees by Department

For Each item In emp(KeyValurPair.Key)

Console.WriteLine(vbTab + item.Name + vbTab + item.Department + vbTab + item.Country)

Next

Next

Console.ReadLine()

End Sub

Class Employee

Public Property Name() As String

Get

Return m_Name

End Get

Set(value As String)

m_Name = Value

End Set

End Property

Private m_Name As String

Public Property Department() As String

Get

Return m_Department

End Get

Set(value As String)

m_Department = Value

End Set

End Property

Private m_Department As String

Public Property Country() As String

Get

Return m_Country

End Get

Set(value As String)

m_Country = Value

End Set

End Property

Private m_Country As String

End Class

End Module

Output of LINQ ToLookup() in Query Syntax Example

Following is the result of LINQ ToLookup() in query syntax example.

 

Output of LINQ ToLookup() Operator Example in Query Syntax

 

This is how we can use LINQ ToLookup() method in c# , vb.net to extra key / value pair from the given list / collection values with query syntax and method syntax.

 
 
 

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User