C# Query Expression
C# Query Expression is an expression that is written by using LINQ query syntax. The LINQ (Language Integrated Query) is a language that is used to construct a query.
C# Query Expression contains set of clauses and use query expression similar to SQL.
Query expression must start with from clause and end with a select or group clause.
To store the query, we must use IEnumerable type variable. It provides a IEnumerator.MoveNext method to iterate elements.
We can iterate elements from the IEnumerable sequence by using two ways.
- IEnumerator.MoveNext method
- foreach loop
Note: We must use System.Linq namespace to execute query expression.
Let’s see an example that displays odd numbers from the array by using the expression query.
C# Query Expression Example 1
Output
1 3 5 7 9
C# Query Expression Example 2
In this example, we are using query expression to fetch student name from the collection of students.
It displays a student, having student id 103.
Output
John