Here, the issue at hand involves the filtration of a set of data objects to obtain a specific collection or sequence of objects while guaranteeing that the sequence adheres to a particular property or condition.
Imagine a group of data entities with numerous attributes. Consider a scenario involving a class representing students and containing a collection of student instances. Each student object includes attributes such as name, age, roll number, address, and email. In this context, if a teacher needs to identify students eligible to vote, they can achieve this by filtering out student names based on an age criterion of 18 years or older.
It is achievable by leveraging the Language Integrated Query in C#. LINQ offers the ability to manipulate collections through various operations like filtering, sorting, aggregation, projection, and more.
Initially, a class needs to be generated followed by instantiating objects of the class and utilizing LINQ functionalities.
Choose the approach for retrieving a set of data objects that meet the specified condition. Furthermore, you have the option to employ LINQ functions such as "Where" and "OrderBy" to manipulate the data collection.
Example 1:
Let's consider a C# program that generates a Filtered Sequence of Elements containing a single property from the provided 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:
The CSS code snippet shown below defines the styling for a placeholder diagram. It includes a linear gradient background, border radius, padding, margin, and text alignment properties for the diagram. The placeholder icon within the diagram is styled with a specific font size and margin, while the placeholder text is styled with a color and font size.
Explanation:
This C# code initiates by declaring the "Employee" class with attributes like "Name", "EmployeeID", and "Salary". Within the main function, four instances of the employee class are instantiated. Subsequently, the LINQ 'Where' function is employed to screen employees based on a salary threshold of over $55,000. The 'Select' method is then applied to generate a new sequence. Lastly, the sequence is traversed to display its contents.
Example 2:
Let's consider a C# code example that demonstrates how to generate a Filtered Sequence of Elements by extracting only a specific property from the provided data using the 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:
The styling for placeholders involves a background color gradient, border radius, padding, margin, and text alignment. The placeholder element includes an icon and text with specific font sizes and colors.
Explanation:
In this software, a class called "Transaction" holds two attributes: "Salesperson" and "Amount". Within the primary function, a series of transactions is generated. Consequently, the program employs the Aggregate method within LINQ to calculate the cumulative sum of all transaction amounts.
Example 3:
Let's consider a sophisticated program designed to generate a Filtered Sequence of Elements that specifically includes just a single attribute from the provided 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:
In the code snippet below, the CSS class .placeholder-diagram sets the background with a linear gradient, rounded corners, padding, margin, and aligns the content at the center. The .placeholder-icon class adjusts the size and margin of the icon, while the .placeholder-text class defines the color and font size of the text within the diagram.
Explanation:
In this scenario, there are two categories named "Product" containing attributes such as "Name," "ProductID," and "Price," and the other class is "InventoryStatus" containing attributes like "ProductID" and "InStock" indicating the availability of a specific product. By leveraging LINQ methods like "Join," "Where," and "Select," the code sieves out the products under $500 and checks their availability. Subsequently, a foreach loop is implemented to display the reasonably priced products.