The Where operator in LINQ is like a filter for a collection (like an array, list, or other enumerable data). It allows you to selectively pick elements from the collection based on a certain condition. Think of it as a way to "query" the collection to get only the items that meet a specific criterion.
Scenario: Imagine you're a manager at a retail store, and you want to find all the products in your inventory that are currently out of stock (have a quantity of 0).
Scenario: You're a content manager for a social media platform, and you want to find all the posts that have received more than 100 likes for further promotion.
A simple breakdown how the Where operator works:
- Collection: You start with a collection of items (e.g., a list of numbers, names, or objects).
- Condition: You specify a condition that you want the items to satisfy. This condition is expressed using a lambda expression, which is like a short piece of code that checks each item.
- Filtering: The Where operator goes through each item in the collection and checks if the condition is true for that item. If the condition is true, the item is included in the result; if not, it's excluded.
- Result: The Where operator returns a new collection containing only the items that passed the condition.
A program that Filter List and Display details of employees who have joined within the last year.
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main(string[] args)
{
// Create a list of employees with their details
List<Employee> employees = new List<Employee>
{
new Employee { Id = 1, Name = "John", IsActive = true, JoinDate = new DateTime(2023, 1, 15) },
new Employee { Id = 2, Name = "Jane", IsActive = true, JoinDate = new DateTime(2022, 8, 10) },
new Employee { Id = 3, Name = "Alex", IsActive = false, JoinDate = new DateTime(2021, 5, 5) },
new Employee { Id = 4, Name = "Emily", IsActive = true, JoinDate = new DateTime(2023, 3, 25) },
new Employee { Id = 5, Name = "Mike", IsActive = false, JoinDate = new DateTime(2022, 8, 5) }
};
// Filter and retrieve employees who have joined within the last year
var recentEmployees = employees
.Where(emp => emp.JoinDate >= DateTime.Now.AddYears(-1))
.ToList();
Console.WriteLine("Employees Joined in last one year");
Console.WriteLine("---------------------------------");
// Display details of recent employees
foreach (var employee in recentEmployees)
{
Console.WriteLine($"ID: {employee.Id}, Name: {employee.Name}, Join Date: {employee.JoinDate}");
}
}
}
class Employee
{
// Properties representing employee details
public int Id { get; set; }
public string? Name { get; set; }
public bool IsActive { get; set; }
public DateTime JoinDate { get; set; }
}
--Output Result
Employees Joined in last one year
---------------------------------
ID: 1, Name: John, Join Date: 15-01-2023 00:00:00
ID: 2, Name: Jane, Join Date: 10-08-2022 00:00:00
ID: 4, Name: Emily, Join Date: 25-03-2023 00:00:00
A program that filters a dictionary of books based on their genres.
using System;
using System.Collections.Generic;
using System.Linq;
class Book
{
public string? Title { get; set; }
public string? Author { get; set; }
public string? Genre { get; set; }
}
class Program
{
static void Main(string[] args)
{
// Create a dictionary representing a library of books
Dictionary<string, Book> library = new Dictionary<string, Book>
{
{ "The Great Gatsby", new Book { Title = "The Great Gatsby", Author = "F. Scott Fitzgerald", Genre = "Classic" } },
{ "To Kill a Mockingbird", new Book { Title = "To Kill a Mockingbird", Author = "Harper Lee", Genre = "Fiction" } },
{ "1984", new Book { Title = "1984", Author = "George Orwell", Genre = "Dystopian" } },
{ "Pride and Prejudice", new Book { Title = "Pride and Prejudice", Author = "Jane Austen", Genre = "Romance" } },
{ "The Hobbit", new Book { Title = "The Hobbit", Author = "J.R.R. Tolkien", Genre = "Fantasy" } }
};
// Using the Where operator to filter books by genre
string targetGenre = "Fantasy";
var fantasyBooks = library.Where(book => book.Value.Genre == targetGenre);
// Display books in the specified genre
Console.WriteLine($"Books in the {targetGenre} Genre:");
Console.WriteLine("----------------------------");
foreach (var book in fantasyBooks)
{
Console.WriteLine($"Title: {book.Value.Title}, Author: {book.Value.Author}");
}
}
}
--Output
Books in the Fantasy Genre:
----------------------------
Title: The Hobbit, Author: J.R.R. Tolkien
Saravanan GanesanPosted Aug 5, 2023, 4:51 PM
Nice and Informative Article Raj Bhatt !!!!