Open In App

C# Program to Demonstrate the Use of the Method as a Condition in the LINQ

Last Updated : 06 Mar, 2022
Like Article

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 demonstrate how to use the method as a condition by taking the example of employee data in which the 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. Or we can say that Where() method returns the value from the sequence based on the specified criteria.


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


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

  1. Create a function named “checkstring” which check the length of the string is less than 4 characters.i.e.,  str.Length < 4.
  2. Create a list(i.e., XEmployee) that will contain the name of the employees.
  3. Add the employee names to the list.
  4. Now find the employee names whose length is less than 4 characters by using “data.Where(employee => checkstring(employee))”.
  5. Display the employee names.



// C# program to illustrate the use of
// the method as a condition in the
// LINQ where() method of the list collection
using System;
using System.Collections.Generic;
using System.Linq;
class GFG{
// Function to check the given string is less than 4
static bool checkstring(string str)
    if (str.Length < 4)
        return true;
        return false;
static void Main(string[] args)
    // Define a list
    List<string> XEmployee = new List<string>();
    // Add names into the list
    // Choose the employee whose name length is
    // less than 4 letters
    IEnumerable<string> result = XEmployee.Where(
                                 employee => checkstring(employee));
    Console.WriteLine("Name of the Employees are: ");
    // Display employee names
    foreach (string stname in result)


Name of the Employees are: 

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads