C# LINQ With Select and SelectMany Operator

Introduction

This article will guide you on LINQ with C# and how to use it using select & select many operators. This is a very important interview question as well for a beginner-level developer. So let's start.

What is LINQ (Language Integrated Query)?

LINQ (Language Integrated Query) is a uniform query syntax in C# and VB.NET to fetch/retrieve data from multiple sources. It is integrated into C# or VB, thereby eliminating the mismatch between programming languages and databases.

LINQ query return results in an object format. Linq provides many query operators like projection, sorting, filtering, aggregation, grouping, etc.

In this article, we will learn about LINQ projection operators. LINQ provides "Select" and "SelectMany" as projection operators.

  1. Select operator: this operator is widely used, and it is used to get value from the collection, which we can use for custom results depending on our requirements.
  2. SelectMany operator: This operator is used to get value from collections of collections.

Class

public class Player {

  public int Id {
    get;
    set;
  }

  public string Name {
    get;
    set;
  }

  public List < string > Skills {
    get;
    set;
  }

  public int CategoryId {
    get;
    set;
  }
}

private static List < Player > CreatePlayerRepository()
{
  List < Player > playersList = new List < Player > () {
    new Player() {
        Id = 1001, Name = "Player1", CategoryId = 1, Skills = new List < string > {
          "Bat",
          "Bowl"
        }
      },

      new Player() {
        Id = 1002, Name = "Player2", CategoryId = 1, Skills = new List < string > {
          "Bat",
          "Bowl",
          "Keeper"
        }
      },

      new Player() {
        Id = 1003, Name = "Player3", CategoryId = 2, Skills = new List < string > {
          "Bat",
          "Bowl",
          "Keeper"

        }
      },

      new Player() {
        Id = 1004, Name = "Player4", CategoryId = 3, Skills = new List < string > {
          "Bowl",
        }
      },
      new Player() {
        Id = 1005, Name = "Player5", CategoryId = 3, Skills = new List < string > {
          "Bat",
          "Bowl"
        }
      },
      new Player() {
        Id = 1006, Name = "Player6", CategoryId = 3, Skills = new List < string > {
          "Bat",
          "Bowl",
          "Keeper"
        }
      }
  };
  return playersList;
}

Main method

static void Main(string[] args) {
  Console.WriteLine("LINQ eample with Select Operator!");
  var playersList = CreatePlayerRepository();
  Console.WriteLine("*********** Exaple for Select Operator ********");
  var selectResult = from player in playersList
  select player;

  foreach(var item in selectResult) {
    Console.WriteLine("Player ID : " + item.Id + " , Name : " + item.Name);
  }
  Console.ReadLine();
}

Output of Select Operator

Console.WriteLine("******** Exapmple for SelectMany Operator *********");
var resuleSelectMany = playersList.SelectMany(player => player.Skills);

foreach(var item in resuleSelectMany) {
  Console.WriteLine(item);
}

LINQ Example of Select Operator

Summary

In this article, we have learned how to use LINQ with C# using select and select many operators. It was a very small article but very important for interview purposes.

Thank you!!


Similar Articles