Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

C# Program to Print the Employees Whose Name Contains Less Than 4 Characters Using LINQ

  • Difficulty Level : Hard
  • Last Updated : 01 Nov, 2021

LINQ is known as Language Integrated Query and it is introduced in .NET 3.5. It gives the ability to .NET languages to generate queries to retrieve data from the data source. It removes the mismatch between programming languages and databases and the syntax used to create a query is the same no matter which type of data source is used. In this article, we will study how to display those employee’s name that contains less than four characters in their name. So to this task, we use Where() function. This function filters the given array according to the given condition.

Syntax:

data.Where(employee => employee.Length < 4)

Example:

Input  : [("m"),("srav"),("a"),("gopi"),("bai")("sai")]
Output : [("m"),("a"),("bai"),("sai")]
 
Input  : [("bobby"),("ramya"),("sairam")]
Output : No Output

Approach

To print the list of employees whose name contains less than 4 characters follow the following steps:

  1. Create a list(i.e., XEmployee) that will conatin the name of the employees.
  2. Add the employee names to the list.
  3. Now find the employee names whose length is less than 4 characters by using data.Where(employee => employee.Length < 4).
  4. Display the employee names.

Example:

C#




// C# program display those employee's name 
// that contains less than 4 characters in their name
using System;
using System.Collections.Generic;
using System.Linq;
  
class GFG{
  
static void Main(string[] args)
{
      
    // Define a list
    List<string> XEmployee = new List<string>();
      
    // Add names into the list
    XEmployee.Add("m");
    XEmployee.Add("srav");
    XEmployee.Add("a");
    XEmployee.Add("gopi");
    XEmployee.Add("bai");
    XEmployee.Add("sai");
  
    // Choose the employee whose name length is
    // less than 4 letters 
    IEnumerable<string> result = XEmployee.Where(
                                 employee => employee.Length < 4);
  
    Console.WriteLine("Name of the Employees are: ");
      
    // Display employee names
    foreach (string stname in result)
    {
        Console.WriteLine(stname);
    }
}
}

Output:

Name of the Employees are: 
m
a
bai
sai




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!