SIGN UP MEMBER LOGIN:    
ARTICLE

Projection Operators: Performing Calculation in the Select Statement

Posted by Akash Ahlawat Articles | LINQ February 20, 2012
In this article we will perform calculation in one of the projection operator i.e. Select operator.
 
Reader Level:

Introduction

Projection operators transform an object into another object with different properties. By using the projection operators, we can construct a new object that has the properties of the objects from which  it has been constructed.

Projection Operators used in LINQ are:

  • Select
  • SelectMany

Select works with one collection whereas SelectMany works with more than one collection. Projection transforms the query result into the form defined by the us.

The Select operator has the opportunity to project a new type of element, however, and its often useful to think of the Select operator as performing a transformation or mapping. 

SelectMany operator will flatten the sub-sequences into a single output sequence we can say SelectMany as something like a nested iterator or nested foreach loop that digs objects out of a sequence inside a sequence.

The below example code demonstrates how to use projections in LINQ to SQL query.

Example

First open the Visual Studio

Then follow the step- File>New>Project then follow the path- Visual C#>Windows>Console Application>OK as shown below:

 Clipboard02.jpg

Then write the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
public class MainClass
{
    public static void Main()

    {
        int[] numbers = { 7, 4, 61, 2, 22 };
        var numsPlusOne =

            from n in numbers
            select n + 1;
 
        Console.WriteLine("Numbers + 1:");

        foreach (var i in numsPlusOne)
        {
            Console.WriteLine(i);
        }
        Console.ReadLine();
    }
}

Output

 Clipboard03.jpg

Resources

How Calculation is performed by SELECT statement in T-SQL
Using Select Statement in SQLite
SELECT Statement in Oracle SQL Plus
How to perform the Rename Operation with Select Statement in SQL
How Where clause is used with Calculation in T-SQL

share this article :
post comment