Updated March 31, 2023
Introduction to LINQ Select
LINQ Select comes under the Projection Operator, the select operator used to select the properties to display/selection. Select operator is mainly used to retrieve all properties or only a few properties which we need to display. It is used to select one or more items from the list of items or from the collection. We can retrieve the result as per our requirement using LINQ Select. By using the Select operator, we can shape the data as per our needs. An operator is used to return an IEnumerable collection of items, which includes the data performed on the method’s transformation.
Syntax of LINQ Select
It is used to select one or more items from the list of items or all the items in the collection.
Let’s see the syntax of both in Query and Method types as follows:
Query Syntax:
var varName = from s in ListName
select new { Name = s.name, ClassName= s.class };
Method Syntax:
var varName = ListName.Select(s => new { Name = s.name ,
ClassName = s.class });
Here we select the name and class of the collection using the select operator in both the methods.
How Select Works in LINQ?
LINQ Select operator is used to return an IEnumerable collection of items, including the data performed on the transformation of the method. By using Select Operator, we can shape the data as per our needs.
In it, we can use two syntax types; let’s see each method working flow.
The statements query syntax always uses the GroupBy or Select clause; there is a sample code that returns the string collection of Name.
Code:
IList<CustomerClass> CustomerList= new List<CustomerClass>()
{
new CustomerClass() { Customer_ID = 1001, Customer_Name = "Smith" },
new CustomerClass() { Customer_ID = 1002, Customer_Name = "Mercy" },
new CustomerClass() { Customer_ID = 1003, Customer_Name = "Bridean" },
new CustomerClass() { Customer_ID = 1004, Customer_Name = "Rachel" },
new CustomerClass() { Customer_ID = 1005, Customer_Name = "Don" }
};
var querySyntax = from s in CustomerList
select s.Customer_Name;
In the above code, it displays the customer name totally present in the list items. The selection operator was used to retrieve the data as our requirement. The select is used to return the collection of items includes the properties as per our requirement.
Code:
IList<CustomerClass> CustomerList= new List<CustomerClass>() {
new CustomerClass() { Customer_ID = 1001, Customer_Name = "Smith", CustomerAddress = 13, Sri Colony, Chennai. } ,
new CustomerClass() { Customer_ID = 1002, Customer_Name = "Mercy", CustomerAddress = 21, Spencer Plaza, Chennai } ,
new CustomerClass() { Customer_ID = 1003, Customer_Name = "Bridean", CustomerAddress = 18, Bengaluru } ,
new CustomerClass() { Customer_ID = 1004, Customer_Name = "Rachel" , CustomerAddress = 20, Delhi } ,
new CustomerClass() { Customer_ID = 1005, Customer_Name = "Don" , CustomerAddress = 15, Bengaluru }
};
In this below code, we are using the query syntax to return the collection of objects with the name of the customer and address properties of the customer class.
Code:
Var querySelect = from c in CustomerList
select new { Name = "Mr. " + c.Customer_Name, CustomerAddress = c.CustomerAddress };
By using the foreach loop to display the result of iteration.
Code:
foreach (var val in selectResult)
Console.WriteLine("Customer Name: {0}, Customer Address: {1}", val.Name, val.CustomerAddress);
A select operator using the method Syntax.
By using method syntax also we can retrieve the result; by using the select operator, we can get the result in the desired way we want because it shapes the data. For example, in the above code, we can also implement the same in method syntax to retrieve the collection of objects with the name and address of customer properties.
Code:
Var selectMethodSyntax = CustomerList.Select(c => new { Name = c.Customer_Name ,
CustomerAddress = c.CustomerAddress });
Examples of LINQ Select
Different examples are mentioned below:
Example #1
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console_LINQSelect
{
public class Book_Class
{
public int bookID { get; set; }
public string bookName { get; set; }
public string bookAuthorName{get; set;}
public int bookCost { get; set; }
}
class ProgramLINQ_Select
{
static void Main(string[] args)
{
List<Book_Class> bookItems = new List<Book_Class>()
{
new Book_Class(){bookID=1001, bookName="Time Life Screct",bookAuthorName="Jack", bookCost=710},
new Book_Class(){bookID=1002, bookName="Info Tech", bookAuthorName="Brindon", bookCost=545},
new Book_Class(){bookID=1003, bookName="Brilliant", bookAuthorName="Smith", bookCost=680},
new Book_Class(){bookID=1004, bookName="Success Destination", bookAuthorName="Jhon", bookCost=850},
new Book_Class(){bookID=1005, bookName="Graphics Science", bookAuthorName="James", bookCost=375}
};
Console.WriteLine("\t\t\tLINQ SELECT");
Console.WriteLine("\t\t\t-----------\n");
//Query-Syntax
Console.WriteLine("LINQ SELECT - Using Query Syntax");
Console.WriteLine("\nBook Name \t\tBook Cost\tAuthor Name");
Console.WriteLine("--------------------\t---------\t--------------");
var querySelect = from b in bookItems
where b.bookAuthorName.StartsWith("J")
select new { BookName=b.bookName,
BookCost=b.bookCost,
AuthorName=b.bookAuthorName};
foreach (var get_querySelect in querySelect)
{
Console.WriteLine(get_querySelect.BookName + "\t" + get_querySelect.BookCost + "\t\t" + get_querySelect.AuthorName);
}
Console.WriteLine("--------------------\t---------\t--------------\n\n\n");
//Method-Syntax
Console.WriteLine("LINQ SELECT - Using Method Syntax");
var methodSelect = bookItems.Where(s => s.bookAuthorName.StartsWith("J"))
.Select(s => new
{
BookName = s.bookName,
AuthorName = s.bookAuthorName,
BookCost = s.bookCost
});
Console.WriteLine("\nBook Name \t\tBook Cost\tAuthor Name");
Console.WriteLine("--------------------\t---------\t--------------");
foreach (var get_methodSelect in methodSelect)
{
Console.WriteLine(get_methodSelect.BookName + "\t" + get_methodSelect.BookCost + "\t\t" + get_methodSelect.AuthorName);
}
Console.WriteLine("--------------------\t---------\t--------------");
Console.ReadLine();
}
}
}
Output:
Example #2
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console_LINQSelect
{
public class EmployeeClass
{
public int emp_id { get; set; }
public string emp_name { get; set; }
public int emp_salary { get; set; }
}
class ProgramLINQ_Select
{
static void Main(string[] args)
{
List<EmployeeClass> employeeList = new List<EmployeeClass>()
{
new EmployeeClass() { emp_id = 1001, emp_name = "Smith", emp_salary = 30000},
new EmployeeClass() { emp_id = 1002, emp_name = "Sasha", emp_salary = 27000},
new EmployeeClass() { emp_id = 1003, emp_name = "Ricky", emp_salary = 55000},
new EmployeeClass() { emp_id = 1004, emp_name = "Peter", emp_salary = 95000}
};
Console.WriteLine("\t\t\tLINQ SELECT");
Console.WriteLine("\t\t\t-----------\n");
//Query-Syntax
Console.WriteLine("LINQ SELECT - Using Query Syntax");
Console.WriteLine("\nEmployee Name");
Console.WriteLine("---------------");
var result = from emp in employeeList
where emp.emp_salary > 50000
select emp;
foreach(var items in result)
Console.WriteLine(items.emp_name);
Console.WriteLine("---------------\n");
//Method-Syntax
Console.WriteLine("LINQ SELECT - Using Method Syntax");
Console.WriteLine("\nEmployee Name");
Console.WriteLine("---------------");
var methodSelect = employeeList.Where(emp => emp.emp_salary > 50000).Select(emp => new {EmpName=emp.emp_name });
foreach (var items in methodSelect)
Console.WriteLine(items.EmpName);
Console.WriteLine("---------------");
Console.ReadLine();
}
}
}
Output:
Conclusion
In this article, we saw the LINQ Select, which is used to project the data/result. By using select, we can shape our data as per our requirements.
Recommended Articles
This is a guide to LINQ Select. Here we discuss the introduction, how select works in LINQ and examples for better understanding. You may also have a look at the following articles to learn more –