Home » LINQ ToLookup() Method

LINQ ToLookup() Method

by Online Tutorials Library

LINQ ToLookup() Method

ToLookup operator in LINQ is an extension method, and it is used to extract a set of key/value pairs from the source. Here, each element in the resultant collection is a generic Lookup object. Lookup object holds the Key and subsequence items that matched with the Key.

Syntax of LINQ ToLookup Operator

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

C# Code

In the above syntax, we are converting the collection of “objEmployee” to key/value pair list using the ToLookup operator.

Example of LINQ ToLookup() Operator in method syntax

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

C# Code

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

OUTPUT:

LINQ ToLookup() Method

Example of LINQ ToLookup() in Query Syntax

OUTPUT:

LINQ ToLookup() Method


Next TopicLINQ Cast() Method

You may also like