Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
LINQ with custom objects
📖 Scenario: You work in a bookstore and want to organize a list of books by their price. Each book has a title and a price.
🎯 Goal: You will create a list of book objects, set a price limit, use LINQ to find books cheaper than that limit, and then print their titles.
📋 What You'll Learn
Create a Book class with Title and Price properties
Create a list of Book objects with exact titles and prices
Create a priceLimit variable to filter books
Use LINQ query with where to select books cheaper than priceLimit
Print the titles of the filtered books
💡 Why This Matters
🌍 Real World
Filtering and selecting items from a list of custom objects is common in apps like online stores, libraries, or inventory systems.
💼 Career
Understanding LINQ with custom objects helps you write clean, readable code to query data collections efficiently in many C# jobs.
Progress0 / 4 steps
1
Create the Book class and list of books
Create a Book class with Title and Price properties. Then create a list called books with these exact entries: "C# Basics" priced 30, "LINQ in Action" priced 45, "ASP.NET Core" priced 50, "Entity Framework" priced 40.
C Sharp (C#)
Hint
Define a class with two properties. Then create a list and add new Book objects with the exact titles and prices.
2
Set the price limit
Inside the Main method, create an integer variable called priceLimit and set it to 45.
C Sharp (C#)
Hint
Just create an integer variable named priceLimit and assign it the value 45.
3
Use LINQ to find books cheaper than priceLimit
Use a LINQ query with var cheapBooks = from book in books where book.Price < priceLimit select book; inside the Main method, after defining priceLimit.
C Sharp (C#)
Hint
Use a LINQ query expression with from, where, and select to filter books cheaper than priceLimit.
4
Print the titles of the filtered books
Use a foreach loop to print the Title of each book in cheapBooks.
C Sharp (C#)
Hint
Use a foreach loop to go through cheapBooks and print each book's Title with Console.WriteLine.
Practice
(1/5)
1. What does LINQ primarily help you do with custom objects in C#?
easy
A. Create new classes automatically
B. Compile code faster
C. Filter, sort, and select data easily
D. Manage memory manually
Solution
Step 1: Understand LINQ's purpose
LINQ is designed to query collections like lists of objects easily.
Step 2: Identify LINQ's main features
It helps filter, sort, and select data without manual loops.
Final Answer:
Filter, sort, and select data easily -> Option C
Quick Check:
LINQ = Filter, sort, select [OK]
Hint: Remember LINQ is for querying data collections [OK]
Common Mistakes:
Thinking LINQ creates classes
Confusing LINQ with compilation
Assuming LINQ manages memory
2. Which of the following is the correct syntax to select all names from a list of Person objects using LINQ?
easy
A. var names = people.Select(p => p.Name());
B. var names = people.Select(p.Name);
C. var names = people.Select(p => p);
D. var names = people.Select(p => p.Name);
Solution
Step 1: Understand Select syntax
Select expects a lambda expression to pick a property, like p => p.Name.
Step 2: Check each option
var names = people.Select(p => p.Name); uses correct lambda syntax. var names = people.Select(p.Name); misses lambda. var names = people.Select(p => p); selects whole object. var names = people.Select(p => p.Name()); wrongly calls Name as method.
Final Answer:
var names = people.Select(p => p.Name); -> Option D
Quick Check:
Select needs lambda with property [OK]
Hint: Use lambda syntax p => p.Property for Select [OK]
Common Mistakes:
Omitting lambda arrow =>
Calling property as method
Selecting whole object instead of property
3. Given the class Person { public string Name; public int Age; } and list people with three persons: Alice(30), Bob(25), and Carol(35), what does this LINQ query return?
var result = people.Where(p => p.Age > 28).Select(p => p.Name).ToList();
medium
A. ["Alice", "Carol"]
B. ["Bob"]
C. ["Alice", "Bob", "Carol"]
D. Empty list
Solution
Step 1: Filter people older than 28
Alice is 30 (yes), Bob is 25 (no), Carol is 35 (yes).
Step 2: Select their names
Names selected are "Alice" and "Carol".
Final Answer:
["Alice", "Carol"] -> Option A
Quick Check:
Age > 28 filters Alice and Carol [OK]
Hint: Filter first, then select property [OK]
Common Mistakes:
Including Bob who is younger
Selecting whole object instead of names
Confusing > with <
4. What is wrong with this LINQ query?
var adults = people.Where(p => p.Age >= 18).Select(p => p.Name); foreach(var name in adults) Console.WriteLine(name);
medium
A. The query is correct and will print all names of adults
B. The lambda expression syntax is incorrect
C. The query is missing ToList() or ToArray(), so it won't compile
D. The Where clause should be after Select
Solution
Step 1: Check LINQ query syntax
Where and Select are used correctly with proper lambda syntax.
Step 2: Check foreach usage
LINQ returns IEnumerable<string>, which foreach can iterate without ToList().
Final Answer:
The query is correct and will print all names of adults -> Option A
Quick Check:
IEnumerable works with foreach directly [OK]
Hint: IEnumerable can be iterated without ToList() [OK]
Common Mistakes:
Thinking ToList() is mandatory before foreach
Misplacing Where and Select order
Incorrect lambda syntax
5. You have a list of Product objects with properties Name (string) and Price (decimal). How do you create a dictionary with product names as keys and prices as values, but only include products costing more than 50 using LINQ?
hard
A. products.ToDictionary(p => p.Name, p => p.Price).Where(p => p.Value > 50);
B. products.Where(p => p.Price > 50).ToDictionary(p => p.Name, p => p.Price);
C. products.Select(p => new {p.Name, p.Price > 50}).ToDictionary(p => p.Name, p => p.Price);
D. products.Where(p => p.Price > 50).Select(p => p.Name).ToDictionary();
Solution
Step 1: Filter products with price > 50
Use Where to keep only products costing more than 50.
Step 2: Convert filtered list to dictionary
Use ToDictionary with key selector p.Name and value selector p.Price.
Final Answer:
products.Where(p => p.Price > 50).ToDictionary(p => p.Name, p => p.Price); -> Option B
Quick Check:
Filter then ToDictionary with key and value selectors [OK]
Hint: Filter first, then use ToDictionary with key and value [OK]
Common Mistakes:
Trying to filter after ToDictionary (invalid)
Selecting anonymous types instead of original objects