0
0
C Sharp (C#)programming~30 mins

Where clause filtering in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Filtering Products with a WHERE Clause in C#
📖 Scenario: You work at a small store that sells products. You have a list of products with their prices. You want to find only the products that cost less than $20 to decide what to put on sale.
🎯 Goal: Build a simple C# program that stores product names and prices, then uses a WHERE clause to filter and show only products cheaper than $20.
📋 What You'll Learn
Create a dictionary called products with these exact entries: "Pen": 5, "Notebook": 15, "Backpack": 45, "Pencil": 3, "Eraser": 2
Create an integer variable called priceLimit and set it to 20
Use a LINQ Where clause with priceLimit to filter products for items with price less than priceLimit
Print the filtered products in the format "Product: Price"
💡 Why This Matters
🌍 Real World
Filtering products by price is common in stores to create sales or promotions.
💼 Career
Understanding how to filter data with WHERE clauses is essential for database queries and data processing in many programming jobs.
Progress0 / 4 steps
1
Create the products dictionary
Create a dictionary called products with these exact entries: "Pen": 5, "Notebook": 15, "Backpack": 45, "Pencil": 3, "Eraser": 2
C Sharp (C#)
Need a hint?

Use Dictionary<string, int> and initialize it with the exact product-price pairs.

2
Add the price limit variable
Create an integer variable called priceLimit and set it to 20
C Sharp (C#)
Need a hint?

Declare int priceLimit = 20; inside the Main method.

3
Filter products using a WHERE clause
Use a LINQ Where clause with priceLimit to filter products for items with price less than priceLimit. Store the result in a variable called filteredProducts
C Sharp (C#)
Need a hint?

Use var filteredProducts = products.Where(product => product.Value < priceLimit);

4
Print the filtered products
Use a foreach loop to print each product and its price from filteredProducts in the format "Product: Price"
C Sharp (C#)
Need a hint?

Use foreach (var product in filteredProducts) and inside the loop print Console.WriteLine($"{product.Key}: {product.Value}");