Query Expression - C# Tutorial
C# Course / Version Features / Query Expression

Query Expression

BLUF: Mastering Query Expression is essential for building robust applications with the .NET ecosystem. This tutorial provides clear explanations and practical examples to help you understand and apply this C# concept.
Enterprise Development Tip: Query Expression

C# is a powerful, modern language for enterprise solutions. Discover how Query Expression enhances your development workflow in the guide below.

C# Query Expression is a form of expression crafted using LINQ query syntax. LINQ (Language Integrated Query) serves as a tool to build queries.

C# Query Expression consists of a series of clauses and employs a syntax that resembles SQL queries.

A query expression should commence with a from clause and conclude with a select or group clause.

To retain the query, an IEnumerable type variable must be employed. This variable offers an IEnumerator.MoveNext function for traversing elements.

We have two methods to iterate through elements in the IEnumerable sequence:

  • Utilizing the IEnumerator.MoveNext method
  • Employing the foreach loop
  • Note: We must use System.Linq namespace to execute query expression.

Let's examine an example demonstrating how to filter and display odd numbers from an array using the expression query.

C# Query Expression Example 1

Example

using System;
using System.Linq;
using System.Collections.Generic;
namespace CSharpFeatures
{
    class DelegateInference
    {
        static void Main(string[] args)
        {           
            int[] IntVal = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            // Query Expression
            IEnumerable<int> OddVal = 
                from val in IntVal 
                where (val % 2) != 0 
                select val;
            // Iterating fetched result
            foreach (int val in OddVal)
            {
                Console.WriteLine(val);
            }
        }
    }
}

Output

Output

1
3
5
7
9

C# Query Expression Example 2

In this instance, we are utilizing a query expression to retrieve the student's name from the students' collection.

Example

using System;
using System.Linq;
using System.Collections.Generic;
namespace CSharpFeatures
{
    class Student
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public static List<Student> GetStudents()
        {
            List<Student> student = new List<Student>();
            Student student1 = new Student { ID = 101, Name = "Irfan", Email = "[email protected]"};
            Student student2 = new Student { ID = 102, Name = "Rahul", Email = "[email protected]" };
            Student student3 = new Student { ID = 103, Name = "John",  Email = "[email protected]" };
            student.Add(student1);
            student.Add(student2);
            student.Add(student3);
            return student;
        }
    }
    class QueryExpressionExample
    {
        static void Main(string[] args)
        {
            IEnumerable<string> queryName =
                from student in Student.GetStudents()
                where student.ID == 103
                select student.Name;
            foreach (var s in queryName)
                Console.Write(s);
        }
    }
}

It displays a student, having student id 103.

Output

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience