Updated on Kisan Patel
You can use the into
keyword to store the results of a select, group, or join statement in a temporary variable.
You might use this construction when you need to execute additional queries over the results. Because of this behavior, this keyword is also called a continuation clause.
For Example,
var query = from p in persons where p.Name.StartsWith("K") select p into pNames where pNames.Name.Length > 4 select pNames;
Common use of into
is with grouping.
using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApp { class Program { static void Main(string[] args) { Listpersons = new List { new Person{ Name = "Kisan", City="Ladol" }, new Person{ Name = "Kite", City = "Ladol" }, new Person{ Name = "Ketul", City = "Vijapur" }, new Person{ Name = "Kisan", City = "Vijapur" }, }; var query = from p in persons where p.Name.StartsWith("K") select p into pNames where pNames.Name.Length > 4 select pNames; foreach (var name in query) Console.WriteLine(name.Name + " - " + name.City); } } public class Person { public string Name { get; set; } public string City { get; set; } } }
the output of the above C# program…
Here is another example of how to use “into” keyword with join?
using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApp { class Program { static void Main(string[] args) { Listcustomers = new List { new Customer{ Id = 1, Name = "Kisan", City="Ladol" }, new Customer{ Id = 2, Name = "Kite", City = "Ladol" }, new Customer{ Id = 3, Name = "Ketul", City = "Vijapur" }, new Customer{ Id = 4, Name = "Kisan", City = "Vijapur" }, }; List orders = new List { new Order{ OrderId = 1, CustomerId = 2}, new Order{ OrderId = 2, CustomerId = 3}, new Order{ OrderId = 3, CustomerId = 2}, new Order{ OrderId = 4, CustomerId = 1}, }; var query = from c in customers join o in orders on c.Id equals o.CustomerId into Purchases from p in Purchases select new { c.Id, c.Name, p.CustomerId }; foreach (var name in query) Console.WriteLine(name.CustomerId + " - " + name.Name); } } public class Customer { public int Id { get; set; } public string Name { get; set; } public string City { get; set; } } public class Order { public int OrderId { get; set; } public int CustomerId { get; set; } } }
the output of the above C# program…