Open In App

C# Program For Producing a Filtered Sequence of Elements that Contain Only One Property of Given Data

Last Updated : 20 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a list that contains the details of the students, now we produce a filtered sequence of elements that contain only one property of each Student. So we use the following method of LINQ to solve the given problem:

1. Aggregate() method: This method is used to perform aggregation operations on the values of a collection. In simple words, this method is used to implements a number of operations for each of the elements in the specified collection by keeping the track of the actions that have been done before. For example, the aggregation function is used to calculate the annual rainfall that occurred in 2021 in step with readings collected the entire year. Another example is the product function is used to calculate the product of the values specified in an array.

Syntax:

result = collection.Aggregate((element1, element2) => element1 operation element2);

2. Select clause: The select clause is specifically used to return the IEnumerator collection and this collection holds a number of items that rely on a transformation function. In simple words, we can say that a select operator should be used if the programmer wants to pick or select a single value from the given collection. For a query expression, the select clause what type of values must be selected when a query is made. Note that a query expression is required to be terminated with either a group clause or select clause.

Syntax:

 IEnumerable<int> collection =

           from element in collection

           where expression

           select element;

Here, the collection represents an IEnumerable collection and expression represents a Condition expression.

Example: In this C# program, we are producing a filtered sequence of elements that have only one property of each Student by using the Select Clause LINQ. Also, we are selecting only those elements that satisfy certain conditions. For this purpose, we have used IEnumerable Interface in order to check the condition that the value of the student.Roll_No must be greater than 2. It is possible through iteration over a non-generic collection.

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 contains 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<int> 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 contains 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<Student> students = new List<Student>() {
   
    // Assigning students information
    new Student{ First = "Bhuwanesh", Last = "Nainwal", Roll_No = 1,
                 Marks = new List<int>(){ 97, 92, 81, 60 } },
    new Student{ First = "Harshit", Last = "Nainwal", Roll_No = 2,
                 Marks = new List<int>(){ 75, 84, 91, 39 } },
    new Student{ First = "Shivani", Last = "Mehta", Roll_No = 3,
                 Marks = new List<int>(){ 88, 94, 65, 91 } },
    new Student{ First = "Neha", Last = "Singh", Roll_No = 4,
                 Marks = new List<int>(){ 97, 89, 85, 82 } },
};
 
List<Information> contactList = new List<Information>() {
    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<String> 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();
}
}


Output

student Query:
Mehta
Singh

Time complexity: O(n). // n is the number of elements in the list.

Space complexity: O(n+m). // m is the number of selected elements. 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads