In LINQ, the 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 the LINQ select projection operator in query syntax to get data from the collection.
LINQ Select Query Syntax in C#
var result = from u in userslist
select u;
LINQ Select Query Syntax in VB.NET
Dim result = From s In Objstudent
Select s
LINQ Select Operator in Query Syntax Example
Following is the example of using the 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 the 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 a 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 a collection, it is necessary to iterate over it to access the values. So using a foreach loop, we can access the data and display it on the console. Now we will run and see the output.
LINQ Select Operator Example Output
Following is the result of using the 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 the LINQ select projection operator with method syntax to get the data from a 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 { 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 = Objstudent.[Select](Function(student) New With {
.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 the 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 a 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 the LINQ select operator in the 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 the collection list.