Javatpoint Logo
Javatpoint Logo

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

In this article, we will discuss the use of the method as a condition in the LINQ. LINQ stands for Language Integrated Query, and it was launched in version .NET 3.5. It enables .NET languages to produce commands used to come up with the queries to retrieve data from the data source. It eliminates the gap between programming languages and databases, and using the same syntax, the user accomplishes their task using any data source, be it structured or unstructured. Here, we will represent a use case for which the method will function as a check by using an employee data example with names less than four characters long. Therefore, we will use the Where() function. This method separates from the given array items that meet the given condition. Here, the Where() function is used to select the specific element based on the specified conditions.

What is LINQ?

LINQ is Method Syntax, and it calls the extension methods Enumerable or Queryable from a static class Enumerable or Queryable. It is also known as Method Extension Syntax or Functional Fluent as well. A compiler can change the query syntax to the method syntax at the time of compiling. It is capable of executing the same operators like Select, Where, GroupBy, Join, Max just like the standard operators do; and therefore, we are allowed to call them directly without the need for query sync.

Example 1:

Input: [("a"),("sam"), ("Ravi"),("sai")]

output: [("a"),("sam"),,("sai")]

Example 2:

Input: [("zitar"),("sita"), ("Raghu"),("south")]

output: No Output

Approach:

In order to print the list of employees whose names contain less than 3 characters, follow the following steps:

  • Write a function called "checkstring" to get the length of the string that is less than 3 characters, i.e., Length<3.
  • After that, define a list(XEmployee) and have that list as store the names of employees.
  • A name list that includes the names of employees should be included.
  • Now let's find employees that have a name that is less than 3 characters using the "Where(employee => checkstring(employee))" sentence. Display the employee names.

Example:

Let us take a program to implement the LINQ where() method in C#.

Output:

Name of the Employees are: 
m
go
le
by

Explanation:

The Code begins with the LinqOp class line declaring. After that, we can have a static method checkstatic: this method accepts a string parameter strs and returns the value False if strs has a length of less than 3 characters. For less than 3, it stands for true, whereas for more, it gives false. The Main method declares a List<string> object named Xemp, which will be used to store an emp list of the employee's names. The same elements are added to the Xemp list. After that, we use LINQ to print the list of employees' names. Where method a condition for the filter is passed. Here, it applies a lambda expression to delegate the task to the function employee => checkstring(employee). This lambda expression gives the employee name as the input, checks the string length and then calls the checkstring method that checks to ensure the number of characters is less than 3. If the condition is true, it will be added to the result. We have the filtered result in the var res that is an IEnumerable<string> variable. The code then proceeds to print a message "Name of the Employees are:". For each iteration of a loop, a foreach loop is used to iterate over each name of res collection and print it to the console as Console.WriteLine.

Conclusion:

In conclusion, the program in C# showed LINQ (Language Integrated Query) applicability with filtering and displaying a categorized list of employee names that match the given condition. The program draws the Where() method, an intrinsic of LINQ, to single-handedly bring out elements in a list of names where the length of the name is shorter than 3 characters. The procedure associated with the checkstring is the condition of filtering.

The example illustrates the role the given project plays in getting rid of all the names that are made up of only 3 characters or less; it exemplifies the advantage of LINQ in terms of implementation and readability. At the end, the output exemplifies the names dealing with the given task. This program demonstrates the real-life use of LINQ by demonstrating the capability of LINQ to perform and present data retrieval tasks in plain language.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news