LINQ Select Projection Operator

  Total Views : 334
  Comments : 0
 
  Keywords : Projection in linq with select operator example, Linq select operator in c# / vb.net with example, How to use linq select projection operators in c# / vb.net with example

Here we will learn linq select projection operator in c#, vb.net with example and how to use linq select projection operator with query syntax and method syntax in c#, vb.net to get data from list with examples.

LINQ Select Projection Operator

In LINQ Select projection operator is used to select data from a collection. This LINQ select operator is same as SQL select clause.

LINQ Select in Query Syntax

Following is the syntax of using LINQ select projection operator in query syntax to get data from collection.

 

C# Code

 

var result = from u in userslist

select u;

VB.NET Code

 

Dim result = From s In Objstudent

Select s

LINQ Select Operator in Query Syntax Example

Following is the example of using LINQ select projection operator in query syntax.

 

C# Code

 

using System;

using System.Collections.Generic;

using System.Linq;

 

namespace Linqtutorials

{

class Program

{

static void Main(string[] args)

{

List<Student> Objstudent = new List<Student>()

{

new Student() { StudentId = 1, Name = "Suresh", Marks = 500 },

new Student() { StudentId = 2, Name = "Rohini", Marks = 300 },

new Student() { StudentId = 3, Name = "Madhav", Marks = 400 },

new Student() { StudentId = 4, Name = "Sateesh", Marks = 550 },

new Student() { StudentId = 5, Name = "Praveen", Marks = 600 },

new Student() { StudentId = 6, Name = "Sudheer", Marks = 700 },

new Student() { StudentId = 7, Name = "Prasad", Marks = 550 }

};

var result = from s in Objstudent

select new {SName =s.Name,SID = s.StudentId,SMarks = s.Marks };

foreach (var item in result)

{

Console.WriteLine("The StudentName is {0} ID is {1} Marks is {2}", item.SName, item.SID, item.SMarks);

}

Console.ReadLine();

}

}

class Student

{

public int StudentId { get; set; }

public string Name { get; set; }

public int Marks { get; set; }

}

}

VB.NET Code

 

Module Module1

Sub Main()

Dim Objstudent As New List(Of Student)() From {

New Student() With {.StudentId = 1, .Name = "Suresh", .Marks = 500},

New Student() With {.StudentId = 2, .Name = "Rohini", .Marks = 300},

New Student() With {.StudentId = 3, .Name = "Madhav", .Marks = 400},

New Student() With {.StudentId = 4, .Name = "Sateesh", .Marks = 550},

New Student() With {.StudentId = 5, .Name = "Praveen", .Marks = 600},

New Student() With {.StudentId = 6, .Name = "Sudheer", .Marks = 700},

New Student() With {.StudentId = 7, .Name = "Prasad", .Marks = 550}

}

Dim result = From s In Objstudent

Select New With {

.SName = s.Name, .SID = s.StudentId, .SMarks = s.Marks

}

For Each item In result

Console.WriteLine("The StudentName is {0} ID is {1} Marks is {2}", item.SName, item.SID, item.SMarks)

Next

Console.ReadLine()

End Sub

 

Class Student

Public Property StudentId() As Integer

Get

Return m_StudentId

End Get

Set(value As Integer)

m_StudentId = value

End Set

End Property

Private m_StudentId As Integer

Public Property Name() As String

Get

Return m_Name

End Get

Set(value As String)

m_Name = Value

End Set

End Property

Private m_Name As String

Public Property Marks() As Integer

Get

Return m_Marks

End Get

Set(value As Integer)

m_Marks = Value

End Set

End Property

Private m_Marks As Integer

End Class

End Module

If you observe above code we select the students by creating an anonymous type. In anonymous type we can define our own names just like we defined “SName”,”SID”,”SMarks”. Here we used new keyword to select only required columns instead of selecting all the fields. Finally we are storing data in a variable “result” of type var. As it is collection so it is necessary to iterate over it to access the values. So using a foreach loop we can access the data and display on console. Now we will run and see the output.

LINQ Select Operator Example Output

Following is the result of using LINQ select projection operator with query syntax example.

 

The StudentName is Suresh ID is 1 Marks is 500

The StudentName is Rohini ID is 2 Marks is 300

The StudentName is Madhav ID is 3 Marks is 400

The StudentName is Sateesh ID is 4 Marks is 550

The StudentName is Praveen ID is 5 Marks is 600

The StudentName is Sudheer ID is 6 Marks is 700

The StudentName is Prasad ID is 7 Marks is 550

LINQ Select Operator in Method Syntax

Following is the example of using LINQ select projection operator with method syntax to get the data from collection.

 

C# Code

 

using System;

using System.Collections.Generic;

using System.Linq;

 

namespace Linqtutorials

{

class Program

{

static void Main(string[] args)

{

List<Student> Objstudent = new List<Student>()

{

new Student() { StudentId = 1, Name = "Suresh", Marks = 500 },

new Student() { StudentId = 2, Name = "Rohini", Marks = 300 },

new Student() { StudentId = 3, Name = "Madhav", Marks = 400 },

new Student() { StudentId = 4, Name = "Sateesh", Marks = 550 },

new Student() { StudentId = 5, Name = "Praveen", Marks = 600 },

new Student() { StudentId = 6, Name = "Sudheer", Marks = 700 },

new Student() { StudentId = 7, Name = "Prasad", Marks = 550 }

};

var result = Objstudent.Select(student => new

{

SName = student.Name,

SID = student.StudentId,

SMarks = student.Marks

 

});

foreach (var item in result)

{

Console.WriteLine("The StudentName is {0} ID is {1} Marks is {2}", item.SName, item.SID, item.SMarks);

}

Console.ReadLine();

}

}

class Student

{

public int StudentId { getset; }

public string Name { getset; }

public int Marks { getset; }

}

}

VB.NET Code

 

Module Module1

 

Sub Main()

Dim Objstudent As New List(Of Student)() From {

New Student() With {.StudentId = 1, .Name = "Suresh", .Marks = 500},

New Student() With {.StudentId = 2, .Name = "Rohini", .Marks = 300},

New Student() With {.StudentId = 3, .Name = "Madhav", .Marks = 400},

New Student() With {.StudentId = 4, .Name = "Sateesh", .Marks = 550},

New Student() With {.StudentId = 5, .Name = "Praveen", .Marks = 600},

New Student() With {.StudentId = 6, .Name = "Sudheer", .Marks = 700},

New Student() With {.StudentId = 7, .Name = "Prasad", .Marks = 550}

}

Dim result = Objstudent.[Select](Function(student) NewWith {

.SName = student.Name,

.SID = student.StudentId,

.SMarks = student.Marks

 

})

For Each item In result

Console.WriteLine("The StudentName is {0} ID is {1} Marks is {2}", item.SName, item.SID, item.SMarks)

Next

Console.ReadLine()

End Sub

 

Class Student

Public Property StudentId() As Integer

Get

Return m_StudentId

End Get

Set(value As Integer)

m_StudentId = value

End Set

End Property

Private m_StudentId As Integer

Public Property Name() As String

Get

Return m_Name

End Get

Set(value As String)

m_Name = Value

End Set

End Property

Private m_Name As String

Public Property Marks() As Integer

Get

Return m_Marks

End Get

Set(value As Integer)

m_Marks = Value

End Set

End Property

Private m_Marks As Integer

End Class

End Module

If you observe above code we select the students by creating an anonymous type. In anonymous type we can define our own names just like we defined “SName”,”SID”,”SMarks”. Here we used new keyword to select only required columns instead of selecting all the fields. Now we will run and see the output.

Output of LINQ Select Operator in Method Syntax Example

Following is the result of linq select operator in method syntax example.

 

The StudentName is Suresh ID is 1 Marks is 500

The StudentName is Rohini ID is 2 Marks is 300

The StudentName is Madhav ID is 3 Marks is 400

The StudentName is Sateesh ID is 4 Marks is 550

The StudentName is Praveen ID is 5 Marks is 600

The StudentName is Sudheer ID is 6 Marks is 700

The StudentName is Prasad ID is 7 Marks is 550

This is how we can use linq select projection operator in applications to select or get data from colleciton list.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User