C# into keyword

The into keyword in C# LINQ is used to create intermediate query expressions. It allows you to continue a query after a select or group clause by introducing a new range variable that represents the results of the previous query operation.

Syntax

Following is the syntax for using the into keyword in LINQ queries −

from element in collection
select expression into newVariable
where condition
select finalExpression

The into keyword can also be used with group operations −

from element in collection
group element by key into groupVariable
where groupCondition
select groupExpression

How It Works

The into keyword creates a continuation that allows you to perform additional operations on the results of a previous query clause. It essentially starts a new query segment where the previous results become the new data source.

LINQ Query with 'into' Continuation Initial Query select ... into newVariable Continued Query from e in data where condition select e into temp where temp.Property select temp Intermediate results stored in 'temp' variable Continue querying with additional conditions

Using 'into' with Select Clause

Example

using System;
using System.Linq;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      IList<Employee> employee = new List<Employee>() {
         new Employee() { EmpID = 1, EmpName = "Tom", EmpMarks = 90, Rank = 8} ,
         new Employee() { EmpID = 2, EmpName = "Anne", EmpMarks = 60, Rank = 21 } ,
         new Employee() { EmpID = 3, EmpName = "Jack", EmpMarks = 76, Rank = 18 } ,
         new Employee() { EmpID = 4, EmpName = "Amy" , EmpMarks = 67, Rank = 20} ,
      };

      // Fetching employee name that ends with 'k' and rank is <20 and >5
      var res = from e in employee 
                where e.Rank > 5 && e.Rank < 20 
                select e into filteredEmp 
                where filteredEmp.EmpName.EndsWith("k") 
                select filteredEmp;

      foreach (var emp in res) {
         Console.WriteLine("Name: " + emp.EmpName);
         Console.WriteLine("Marks: " + emp.EmpMarks);
      }
   }
}

public class Employee {
   public int EmpID { get; set; }
   public string EmpName { get; set; }
   public int EmpMarks { get; set; }
   public int Rank { get; set; }
}

The output of the above code is −

Name: Jack
Marks: 76

Using 'into' with Group Clause

Example

using System;
using System.Linq;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      IList<Employee> employees = new List<Employee>() {
         new Employee() { EmpID = 1, EmpName = "Tom", Department = "IT", Salary = 50000} ,
         new Employee() { EmpID = 2, EmpName = "Anne", Department = "HR", Salary = 45000 } ,
         new Employee() { EmpID = 3, EmpName = "Jack", Department = "IT", Salary = 55000 } ,
         new Employee() { EmpID = 4, EmpName = "Amy", Department = "HR", Salary = 48000} ,
      };

      // Group employees by department and filter groups with average salary > 50000
      var result = from emp in employees
                   group emp by emp.Department into deptGroup
                   where deptGroup.Average(e => e.Salary) > 50000
                   select new { 
                      Department = deptGroup.Key, 
                      AvgSalary = deptGroup.Average(e => e.Salary) 
                   };

      foreach (var group in result) {
         Console.WriteLine($"Department: {group.Department}, Average Salary: {group.AvgSalary}");
      }
   }
}

public class Employee {
   public int EmpID { get; set; }
   public string EmpName { get; set; }
   public string Department { get; set; }
   public double Salary { get; set; }
}

The output of the above code is −

Department: IT, Average Salary: 52500

Common Use Cases

  • Multi-step filtering: Apply initial filters, then continue with additional conditions on the intermediate results.

  • Grouping operations: Group data and then perform aggregate operations or filtering on the groups.

  • Complex transformations: Transform data in one step, then apply further operations on the transformed results.

Conclusion

The into keyword in C# LINQ enables query continuation by creating intermediate results that can be further queried. It is particularly useful for complex multi-step operations, grouping scenarios, and when you need to apply additional filtering or transformation after an initial query operation.

Updated on: 2026-03-17T07:04:35+05:30

182 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements