Javatpoint Logo
Javatpoint Logo

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

Here the problem statement talks about filtering some collection of data objects so that we can get the desired collection or sequence of objects and ensure that the sequence follows some property or condition.

Imagine a collection of data objects with many properties. Let's see an example of a student class with a list of student objects. For each student, there is a name, age, roll number, address, and email. Now that the teacher wants to get the names of the students who have the right to vote, the teacher will filter the student names where the student's age is greater than or equal to 18.

It can be done by using the Language Integrated Query in C#. LINQ will provide the query collections to perform different operations such as filtering, sorting, addition, projection, etc.

First, we must create a class. Then, create the objects of that class and use LINQ's

Select the method to get the collection of data objects that satisfy the property. Additionally, you can use the LINQ methods like "Where", "OrderBy", etc.

Example 1:

Let us take a C# program for producing a Filtered Sequence of Elements that contain only one property of given data.

Output:

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

Explanation:

This C# program first defines the "Employee" class. The properties present in the class are "Name", "EmployeeID", and "Salary". In the main method, four objects for the employee are created. After that, using the LINQ's 'Where' method, we filter the employees such that the salary of the employee exceeds $55,000. Here, the "Select" is used to get the new sequence. Finally, we iterated through the sequence to print the sequence.

Example 2:

Let us take a C# program for producing a Filtered Sequence of Elements that contain only one property of given data using Aggregate Method.

Output:

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

Explanation:

In this program, there is a class named "Transaction" which contains two properties "Salesperson" and "Amount". In the main method list of transactions are created. So, this program used Aggregate method in LINQ to get the total amount of money present in all transactions.

Example 3:

Let us take a complex program for producing a Filtered Sequence of Elements that contain only one property of given data.

Output:

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

Explanation:

In this there are two classes that are "Product" which have the properties like "Name", "ProductID", and "Price", other class is "InventoryStatus" which has properties like "ProductID" and "InStock" which saying whether particular product is available or not. Using the LINQ's "Join", "Where", and "Select" methods it filters the products based on price which are less than 500 dollars and availability. A foreach loop is used to print the affordable products.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news