📜  LINQ |投影算子|全选

📅  最后修改于: 2021-05-29 23:24:52             🧑  作者: Mango

在LINQ中,投影是将对象转换为新形式的操作,该新形式仅包含随后将要使用的那些属性。通过使用投影,开发人员可以创建从每个对象构建的新类型。你被允许项目财产并在其上进行数学函数,你也可以投射原始对象,而不将其转化。

在LINQ中,可以使用以下投影操作:

  1. 选择
  2. 全选

SelectMany运算符

SelectMany运算符返回基于转换函数的值序列,然后将它们变成一个序列。换句话说,可以说,当您要从多个集合中选择值时,或者想要从列表列表中获得结果并希望显示为单个序列时,将使用SelectMany运算符。

查询语法中的SelectMany:在查询语法中,SelectMany运算符的工作是通过使用多个from子句来实现的。如下例所示。

例子:

// C# program to find the languages
// known by 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;
    }
    public List emp_lang
    {
        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, emp_lang = new List{"C#", "VB"} },
                new Employee() {emp_id = 210, emp_name = "Soniya", emp_gender = "Female", emp_hire_date = "22/4/2018", emp_salary = 30000,  emp_lang = new List{ "Java"} },
                new Employee() {emp_id = 211, emp_name = "Rohit", emp_gender = "Male", emp_hire_date = "3/5/2016", emp_salary = 40000,  emp_lang = new List{ "C++", "SQL"} },
                new Employee() {emp_id = 212, emp_name = "Supriya", emp_gender = "Female", emp_hire_date = "4/8/2017", emp_salary = 40000,  emp_lang = new List{"Python", "C", "PHP"} },
                new Employee() {emp_id = 213, emp_name = "Anil", emp_gender = "Male", emp_hire_date = "12/1/2016", emp_salary = 40000,  emp_lang = new List{"HTML", "JQuery"} },
                new Employee() {emp_id = 214, emp_name = "Anju", emp_gender = "Female", emp_hire_date = "17/6/2015", emp_salary = 50000,  emp_lang = new List{"JavaScript", "Perl"} },
        };
  
        // Query to find the languages
        // known by the employee
        var res = from e in emp
                      from e2 in e.emp_lang
                          select e2;
  
        Console.WriteLine("Languages known by all the employees are:");
        foreach(var val in res)
        {
            Console.WriteLine(val);
        }
    }
}
输出:
Languages known by all the employees are:
C#
VB
Java
C++
SQL
Python
C
PHP
HTML
JQuery
JavaScript
Perl

方法语法中的SelectMany Queryable和Enumerable类中都存在SelectMany方法,并且C#和VB.Net语言都支持SelectMany方法。

例子:

// C# program to find the languages
// known by 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;
    }
    public List emp_lang
    {
        get;
        set;
    }
}
  
public 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, emp_lang = new List{"C#", "VB"} },
                new Employee() {emp_id = 210, emp_name = "Soniya", emp_gender = "Female", emp_hire_date = "22/4/2018", emp_salary = 30000,  emp_lang = new List{ "Java"} },
                new Employee() {emp_id = 211, emp_name = "Rohit", emp_gender = "Male", emp_hire_date = "3/5/2016", emp_salary = 40000,  emp_lang = new List{ "C++", "SQL"} },
                new Employee() {emp_id = 212, emp_name = "Supriya", emp_gender = "Female", emp_hire_date = "4/8/2017", emp_salary = 40000,  emp_lang = new List{"Python", "C", "PHP"} },
                new Employee() {emp_id = 213, emp_name = "Anil", emp_gender = "Male", emp_hire_date = "12/1/2016", emp_salary = 40000,  emp_lang = new List{"HTML", "JQuery"} },
                new Employee() {emp_id = 214, emp_name = "Anju", emp_gender = "Female", emp_hire_date = "17/6/2015", emp_salary = 50000,  emp_lang = new List{"JavaScript", "Perl"} },
        };
  
        // Finding the languages known by the employee
        // Using SelectMany method
        var res = emp.SelectMany(a => a.emp_lang);
        Console.WriteLine("Languages known by all the employees are:");
        foreach(var val in res)
        {
            Console.WriteLine(val);
        }
    }
}
输出:
Languages known by all the employees are:
C#
VB
Java
C++
SQL
Python
C
PHP
HTML
JQuery
JavaScript
Perl