Let And Into LINQ Examples

Another set of keywords useful in the LINQ language is the Let and Into keywords.

LetAndIntoSolution LetAndIntoProgram


Program.cs Code Contents

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyLinq
    class Program
        static void Main(string[] args)


        // Private Methods
        private static void useLetKeyword()
            var repository = new EmployeeRepo();

            // Demonstrating a simple use of the LET keyword by making the result lowercase.
            var letQuery =
                from e in repository.GetAll()
                let lname = e.Name.ToLower()
                where lname == "scott"
                select lname;
            Write(letQuery, "****LET Keyword Output");

        private static void useIntoKeyWord()
            var repository = new EmployeeRepo();

            // Demonstrating a simple use of the INTO keyword by finding all the employees with the letter 'S'.
            // Then further querying by selecting all those name that have less than 5 letters.
            var intoQuery =
                from employees in repository.GetAll()
                where employees.Name.StartsWith("S")
                select employees
                    into pEmployees
                    where pEmployees.Name.Length < 5
                    select pEmployees;
            Write(intoQuery, "****INTO Keyword Output****");

        // Simple extension method used to write the name of the queried employees
        static void Write(IEnumerable<Employee> employees, string displayTitle)
            foreach (Employee e in employees)

        // Overloaded extension method for the queried employees.
        static void Write(IEnumerable<string> employee, string displayTitle)

In this example I’ve broken the use of the keywords into two methods. The Let keyword allows the storing results of a query that can be used in subsequent queries. What this does is create a variable and initializes it with the results of the query expression. This is being done in the useLetKeyword method. In my example I’m declaring the lname variable, forcing it to all lower characters, looking for name “scott”, and this value is assigned to the variable.

The Into keyword is a bit different. It creates a temporary variable to store the results of a group, join, or select clause. For example the useIntoKeyWord method. I’m selecting all the employees, using into pEmployees, and using the pEmployees variable to look for all the names greater than five characters.

Then I’m writing the results of the two queries with the Write helper methods.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s