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.
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public class Employee
{
public string Name { get; set; }
public int EmployeeID { get; set; }
public double Salary { get; set; }
}
static void Main(string[] args)
{
List<Employee> employees = new List<Employee>
{
new Employee { Name = "Laxman", EmployeeID = 101, Salary = 50000 },
new Employee { Name = "Sita", EmployeeID = 102, Salary = 60000 },
new Employee { Name = "Ramu", EmployeeID = 103, Salary = 70000 },
new Employee { Name = "Anji", EmployeeID = 104, Salary = 45000 }
};
// Filtered sequence of employee names with salaries above $55,000
IEnumerable<string> highSalaryEmployees = employees
.Where(employee => employee.Salary > 55000)
.Select(employee => employee.Name);
Console.WriteLine("Employees with salaries above $55,000:");
foreach (string employeeName in highSalaryEmployees)
{
Console.WriteLine(employeeName);
}
}
}
Output:
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.
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public class Transaction
{
public string Salesperson { get; set; }
public double Amount { get; set; }
}
static void Main(string[] args)
{
List<Transaction> transactions = new List<Transaction>
{
new Transaction { Salesperson = "Ajay", Amount = 500 },
new Transaction { Salesperson = "vijay", Amount = 700 },
new Transaction { Salesperson = "Sanjay", Amount = 900 },
};
double totalSales = transactions
.Aggregate(0.0, (total, transaction) => total + transaction.Amount);
Console.WriteLine("Total sales amount: $" + totalSales);
}
}
Output:
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.
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public class Product
{
public string Name { get; set; }
public int ProductID { get; set; }
public double Price { get; set; }
}
public class InventoryStatus
{
public int ProductID { get; set; }
public bool InStock { get; set; }
}
static void Main(string[] args)
{
List<Product> products = new List<Product>
{
new Product { Name = "Laptop", ProductID = 101, Price = 1200 },
new Product { Name = "Smartphone", ProductID = 102, Price = 800 },
new Product { Name = "Tablet", ProductID = 103, Price = 400 },
new Product { Name = "Headphones", ProductID = 104, Price = 100 },
new Product { Name = "Smartwatch", ProductID = 105, Price = 300 }
};
List<InventoryStatus> inventoryStatusList = new List<InventoryStatus>
{
new InventoryStatus { ProductID = 101, InStock = true },
new InventoryStatus { ProductID = 102, InStock = true },
new InventoryStatus { ProductID = 103, InStock = false },
new InventoryStatus { ProductID = 104, InStock = true },
new InventoryStatus { ProductID = 105, InStock = true }
};
// Filtered sequence of product names with price less than $500 and in stock
IEnumerable<string> affordableProducts = products
.Join(inventoryStatusList,
product => product.ProductID,
inventory => inventory.ProductID,
(product, inventory) => new { product, inventory })
.Where(pair => pair.product.Price < 500 && pair.inventory.InStock)
.Select(pair => pair.product.Name);
// Output the filtered sequence
Console.WriteLine("Affordable products currently in stock:");
foreach (string productName in affordableProducts)
{
Console.WriteLine(productName);
}
}
}
Output:
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.