BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

LINQ
.NET 3.5+

LINQ Projection

The third part of the LINQ to Objects tutorial describes basic projection in LINQ using the Select standard query operator and the select clause. These allow the type of all of the values that are returned from a LINQ query to be specified.

Projections with Calculations

For the final example of projection using the Select standard query operator, we will include additional operations for the resultant values, again holding the results in an anonymous type. This is particularly useful when you wish to perform the same calculation upon each of the results. The calculations are included within the object initializer for the anonymous type.

In the example, each result contains three properties. The first is a string containing the name and job title for each employee, concatenated using the String.Format method. The second property is the employee's current salary and the final member contains a proposed new salary, being five percent higher than the existing value.

var salaryIncrease = employees.Select(e =>
    new {
        Employee = string.Format("{0}/{1}", e.Name, e.Title),
        e.Salary,
        NewSalary = e.Salary * 1.05
    });
 
/* RESULTS

{ Employee = Bob/Senior Developer, Salary = 40000, NewSalary = 42000 }
{ Employee = Sam/Developer, Salary = 32000, NewSalary = 33600 }
{ Employee = Mel/Developer, Salary = 29000, NewSalary = 30450 }
{ Employee = Jim/Junior Programmer, Salary = 20000, NewSalary = 21000 }

*/

Projection and Query Expression Syntax

In this remainder of the article we will recreate the queries from the previous sections using query expression syntax. To specify the projection, the code following the select clause is changed. The code to use is similar to that from the lambda expressions used above, except that the parameter and lambda operator are omitted.

For example, in the first sample query the names all of the employees in the collection were retrieved. The lambda expression for the Select method was "e=>e.Name". In query expression syntax, the range variable defines the parameter so only, "e.Name", need be specified:

var names =
    from e in employees
    select e.Name;

A where clause can be added to filter the results:

var names =
    from e in employees
    where e.Salary > 30000
    select e.Name;

Projecting into Anonymous Types

The second group of examples projected the name and title of each employee into an anonymous type. The first of these used default names for the properties of the resultant objects. This can be recreated using the new operator and the required properties within braces, as follows:

var namesAndTitles =
    from e in employees
    select new { e.Name, e.Title };

The second anonymous type projection example named the properties in the results. This can also be recreated using query expression syntax:

var namesAndTitles =
    from e in employees
    select new { Employee = e.Name, Job = e.Title };

Projections with Calculations

The final sample query retrieved a list of all of the employees, projected into an anonymous type with three properties. The Employee property contained the name and job title of each employee. The Salary property was directly extracted from the source collection and the NewSalary was calculated. This can be recreated using the same standard syntax, as follows:

var salaryIncrease =
    from e in employees
    select new {
        Employee = string.Format("{0}/{1}", e.Name, e.Title),
        e.Salary,
        NewSalary = e.Salary * 1.05
    };
5 July 2010