In this guide, we will determine the where clause in C#.
How to Use where Clause in C#
In C#, there are various functionalities in which the where clause is used in Language Integrated Query (LINQ). It is used to determine which element from the data source of query expression will be returned. With the help of the C# where clause, programmers can filter data from databases or collections according to predefined criteria.
Note: The where clause is essential for searching and modifying data since it enables developers to just retrieve the pertinent data they require.
Syntax
The basic syntax for the where clause is as follows:
where condition
select item;
In the above code, the “collection” refers to an array, list, or database table, which represents the data source. Then, the boolean expressions are defined by the “condition” that is often used to filter data. The “select” keyword retrieves the items or characteristics from filtered data in query expressions.
Example: Find Positive Items From the Array Using the where Clause
The following code shows the implementation of the where clause to determine how to filter positive values from an array that fall within a certain range using LINQ (Language-Integrated Query).
Let’s discuss the statements of code step by step:
using System.Linq;
Here:
- “Using System” includes the System namespace, containing basic types and base types frequently used in C# programs.
- “Using System.Linq” adds a LINQ namespace and offers extensions and query operations for LINQ-based data collection manipulation.
In the below snippet code, first, we define a class “LinqWhere”. Next, start execution from the “main()” function where we declare an integer type array “num1” and initialize with positive and negative numbers. After this, it shows a message on the screen using the function “Console.Write()” which determines the functionality of the program. Next, we define the LINQ query variable “query1” with the “var” keyword, which creates a range variable “var_Name” that represents every item of the array “num1”. Then, the query “query1” contains two “where” clauses. Moving forward, select the specific variables from “var_Name” with the “select” keyword and save the result in the “query1” variable of the LINQ query.
After that, use the “Console.Write()” function to display the positive integers with array indexes according to a particular range using the “foreach” loop that iterates “query1” elements to print each element one by one:
{
static void Main()
{
int[] num1 = {
1, 2, -2, -4, -7, -5, 8, 12, 19, 6, 4, 15, 19
};
Console.Write("Positive Numbers in LINQ Using Multiple where clause within items of List\n ");
var query1 =
from var_Name in num1
where var_Name > 1
where var_Name < 15
select var_Name;
Console.Write("\nThe positive numbers within 1 to 15 are : ");
foreach(var var_Name in query1)
{
Console.Write("{0}", var_Name);
}
}
}
The resultant output of the above-executed code has been stated below:
You have learned all about the where clause in C#.
Conclusion
In C#, the where clause is used to filter the data. Developers can use it to establish criteria and only get the desired data out of databases, lists, or arrays. In C#, the where clause makes it simple to modify and clean the data. It also provides code readability and enhances performance with dynamic filtration. This tutorial illustrated the where clause in C#.