📌  相关文章
📜  用于生成仅包含给定数据的一个属性的元素的过滤序列的 C# 程序

📅  最后修改于: 2022-05-13 01:54:45.183000             🧑  作者: Mango

用于生成仅包含给定数据的一个属性的元素的过滤序列的 C# 程序

给定一个包含学生详细信息的列表,现在我们生成一个过滤后的元素序列,其中仅包含每个学生的一个属性。所以我们使用下面的LINQ方法来解决给定的问题:

1. Aggregate() 方法该方法用于对集合的值进行聚合操作。简而言之,此方法用于通过跟踪之前已完成的操作,为指定集合中的每个元素实现许多操作。例如,聚合函数用于与全年采集的读数同步计算 2021 年发生的年降雨量。另一个例子是 product函数用于计算数组中指定值的乘积。

句法:

2. Select 子句 select 子句专门用于返回 IEnumerator 集合,该集合包含许多依赖于转换函数的项。简单来说,我们可以说如果程序员想要从给定集合中选择或选择单个值,则应该使用选择运算符。对于查询表达式,select 子句在进行查询时必须选择什么类型的值。请注意,查询表达式必须以 group 子句或 select 子句终止。

句法:

在这里,集合表示一个 IEnumerable 集合,而表达式表示一个 Condition 表达式。

示例:在这个 C# 程序中,我们使用 Select Clause LINQ 生成过滤后的元素序列,这些元素只有每个 Student 的一个属性。此外,我们只选择那些满足特定条件的元素。为此,我们使用了 IEnumerable 接口来检查student.Roll_No的值必须大于 2 的条件。可以通过对非泛型集合进行迭代。

C#
// C# program to producing a filtered sequence of
// elements that have only one property of each 
// Student by using the Select Clause LINQ
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
// Public class GFG
class GFG{
  
// Student class
// Declared using public access specifier
// This class conatins the detail of the students 
public class Student {
    public string First
    {
        get;
        set;
    }
    public string Last
    {
        get;
        set;
    }
    public int Roll_No
    {
        get;
        set;
    }
    public List Marks;
    public Information GetInformation(GFG gfg,
                                      int roll_no)
    {
        Information allinfo = (from ci in gfg.contactList where ci.Roll_No
                            == roll_no select ci).FirstOrDefault();
  
        return allinfo;
    }
  
    public override string ToString()
    {
        return First + "" + Last + " :  " + Roll_No;
    }
}
  
// Information class
// Declared using public access specifier
// This class conatins the personal details 
// of the students
public class Information
{
    public int Roll_No
    {
        get;
        set;
    }
    public string Email
    {
        get;
        set;
    }
    public string Phone
    {
        get;
        set;
    }
    public override string ToString()
    {
        return Email + "," + Phone;
    }
}
  
// This class contains the score of the students
public class ScoreInfo
{
    public double Average
    {
        get;
        set;
    }
    public int Roll_No
    {
        get;
        set;
    }
}
List students = new List() {
    
    // Assigning students information
    new Student{ First = "Bhuwanesh", Last = "Nainwal", Roll_No = 1,
                 Marks = new List(){ 97, 92, 81, 60 } },
    new Student{ First = "Harshit", Last = "Nainwal", Roll_No = 2,
                 Marks = new List(){ 75, 84, 91, 39 } },
    new Student{ First = "Shivani", Last = "Mehta", Roll_No = 3,
                 Marks = new List(){ 88, 94, 65, 91 } },
    new Student{ First = "Neha", Last = "Singh", Roll_No = 4,
                 Marks = new List(){ 97, 89, 85, 82 } },
};
  
List contactList = new List() {
    new Information{ Roll_No = 111, Email = "Bhuwanesh@abc.com",
                     Phone = "6131341379" },
    new Information{ Roll_No = 112, Email = "Harshit@abc.com",
                     Phone = "2131341379" },
    new Information{ Roll_No = 113, Email = "Shivani@abc.com",
                     Phone = "1234561456" },
    new Information{ Roll_No = 114, Email = "Neha@abc.com",
                     Phone = "5131341379" }
};
  
// Driver code
static public void Main()
{
      
    // Instantiate GFG object
    GFG gfg = new GFG();
    
    // Using IEnumerable
    IEnumerable studentQuery2
        = from student in gfg.students where student.Roll_No >
          2 select student.Last;
            
    // Displaying the result
    Console.WriteLine("student Query:");
    foreach(string s in studentQuery2)
    {
        Console.WriteLine(s);
    }
    Console.ReadLine();
}
}


输出
student Query:
Mehta
Singh