在LINQ中,投影是将对象转换为仅包含随后将要使用的那些属性的新形式的操作。通过使用投影,开发人员可以创建从每个对象构建的新类型。你被允许项目财产并在其上进行数学函数,你也可以投射原始对象,而不将其转化。
在LINQ中,可以使用以下投影操作:
- 选择
- 全选
选择运营商
select运算符返回IEnumerator集合,该集合基于转换函数保存项目。换句话说,当您要从给定集合中选择单个值时,可以说使用了选择运算符。
使用查询语法进行选择:在LINQ中,使用查询语法创建查询时使用Select运算符。在查询表达式中,表达式以Select或GroupBy子句结尾。在查询中,select运算符用于返回自定义类或匿名类型的集合,这些集合包括根据用户需要的属性,还用于根据用户需要来操纵结果。使用C#和VB.NET
语言选择运算符支持查询语法,如以下示例所示:
例子:
// C# program to find the name
// of the employee
using System;
using System.Linq;
using System.Collections.Generic;
// Employee details
public class Employee {
public int emp_id
{
get;
set;
}
public string emp_name
{
get;
set;
}
public string emp_gender
{
get;
set;
}
public string emp_hire_date
{
get;
set;
}
public int emp_salary
{
get;
set;
}
}
class GFG {
// Main method
static public void Main()
{
List emp = new List() {
new Employee() { emp_id = 209, emp_name = "Anjita", emp_gender = "Female", emp_hire_date = "12/3/2017", emp_salary = 20000 },
new Employee() { emp_id = 210, emp_name = "Soniya", emp_gender = "Female", emp_hire_date = "22/4/2018", emp_salary = 30000 },
new Employee() { emp_id = 211, emp_name = "Rohit", emp_gender = "Male", emp_hire_date = "3/5/2016", emp_salary = 40000 },
new Employee() { emp_id = 212, emp_name = "Supriya", emp_gender = "Female", emp_hire_date = "4/8/2017", emp_salary = 40000 },
new Employee() { emp_id = 213, emp_name = "Anil", emp_gender = "Male", emp_hire_date = "12/1/2016", emp_salary = 40000 },
new Employee() { emp_id = 214, emp_name = "Anju", emp_gender = "Female", emp_hire_date = "17/6/2015", emp_salary = 50000 },
};
// Query to find the name of the employee
// Using select in Query Syntax
var res = from e in emp
select e.emp_name;
foreach(var val in res)
{
Console.WriteLine("Employee Name: {0}", val);
}
}
}
输出:
Employee Name: Anjita
Employee Name: Soniya
Employee Name: Rohit
Employee Name: Supriya
Employee Name: Anil
Employee Name: Anju
使用方法语法进行选择:在方法语法中,选择运算符是可选的,您可以使用它对数据进行整形。 Select方法同时存在于Queryable和Enumerable类中。它支持C#和VB.Net语言的方法语法。以下示例显示了select方法的使用。
例子:
// C# program to find the name
// and id of the employee
using System;
using System.Linq;
using System.Collections.Generic;
// Employee details
public class Employee {
public int emp_id
{
get;
set;
}
public string emp_name
{
get;
set;
}
public string emp_gender
{
get;
set;
}
public string emp_hire_date
{
get;
set;
}
public int emp_salary
{
get;
set;
}
}
class GFG {
// Main method
static public void Main()
{
List emp = new List() {
new Employee() { emp_id = 209, emp_name = "Anjita", emp_gender = "Female", emp_hire_date = "12/3/2017", emp_salary = 20000 },
new Employee() { emp_id = 210, emp_name = "Soniya", emp_gender = "Female", emp_hire_date = "22/4/2018", emp_salary = 30000 },
new Employee() { emp_id = 211, emp_name = "Rohit", emp_gender = "Male", emp_hire_date = "3/5/2016", emp_salary = 40000 },
new Employee() { emp_id = 212, emp_name = "Supriya", emp_gender = "Female", emp_hire_date = "4/8/2017", emp_salary = 40000 },
new Employee() { emp_id = 213, emp_name = "Anil", emp_gender = "Male", emp_hire_date = "12/1/2016", emp_salary = 40000 },
new Employee() { emp_id = 214, emp_name = "Anju", emp_gender = "Female", emp_hire_date = "17/6/2015", emp_salary = 50000 },
};
// finding the name and id of the
// employee Using select method
var res = emp.Select(a => new { ID = a.emp_id, Name = a.emp_name });
foreach(var val in res)
{
Console.WriteLine("Employee Name : {0} Employee ID : {1}",
val.Name, val.ID);
}
}
}
输出:
Employee Name : Anjita Employee ID : 209
Employee Name : Soniya Employee ID : 210
Employee Name : Rohit Employee ID : 211
Employee Name : Supriya Employee ID : 212
Employee Name : Anil Employee ID : 213
Employee Name : Anju Employee ID : 214