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

LINQ with custom objects in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LINQ Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
LINQ Query Output with Custom Objects

What is the output of the following C# code using LINQ on a list of custom Person objects?

C Sharp (C#)
using System;
using System.Collections.Generic;
using System.Linq;

public class Person {
    public string Name { get; set; }
    public int Age { get; set; }
}

public class Program {
    public static void Main() {
        var people = new List<Person> {
            new Person { Name = "Alice", Age = 30 },
            new Person { Name = "Bob", Age = 25 },
            new Person { Name = "Charlie", Age = 35 },
            new Person { Name = "Diana", Age = 28 }
        };

        var result = people.Where(p => p.Age > 28).OrderBy(p => p.Name).Select(p => p.Name);

        foreach (var name in result) {
            Console.WriteLine(name);
        }
    }
}
A
Alice
Diana
Charlie
B
Charlie
Alice
C
Bob
Diana
D
Alice
Charlie
Attempts:
2 left
💡 Hint

Look at the filter condition Age > 28 and then the ordering by Name.

🧠 Conceptual
intermediate
1:30remaining
Understanding LINQ Select Projection

Given a list of Product objects, which LINQ query correctly projects only the product names into a list of strings?

C Sharp (C#)
public class Product {
    public int Id { get; set; }
    public string Name { get; set; }
    public double Price { get; set; }
}

var products = new List<Product> {
    new Product { Id = 1, Name = "Pen", Price = 1.5 },
    new Product { Id = 2, Name = "Notebook", Price = 3.0 },
    new Product { Id = 3, Name = "Eraser", Price = 0.5 }
};
Avar names = products.Select(p => p.Name).ToList();
Bvar names = products.Where(p => p.Name).ToList();
Cvar names = products.Select(p => p.Price).ToList();
Dvar names = products.OrderBy(p => p.Name).ToList();
Attempts:
2 left
💡 Hint

Projection means selecting a specific property from each object.

🔧 Debug
advanced
2:30remaining
Fix the LINQ Query to Avoid Runtime Error

The following code throws a runtime exception. Which option fixes the code so it runs without error and returns the names of employees older than 40?

C Sharp (C#)
using System;
using System.Collections.Generic;
using System.Linq;

public class Employee {
    public string Name { get; set; }
    public int? Age { get; set; }
}

public class Program {
    public static void Main() {
        var employees = new List<Employee> {
            new Employee { Name = "John", Age = 45 },
            new Employee { Name = "Jane", Age = null },
            new Employee { Name = "Mark", Age = 50 }
        };

        var result = employees.Where(e => e.Age > 40).Select(e => e.Name);

        foreach (var name in result) {
            Console.WriteLine(name);
        }
    }
}
Avar result = employees.Where(e => e.Age != null).Select(e => e.Name);
Bvar result = employees.Where(e => e.Age.HasValue && e.Age > 40).Select(e => e.Name);
Cvar result = employees.Where(e => e.Age.Value > 40).Select(e => e.Name);
Dvar result = employees.Where(e => e.Age > 40).Select(e => e.Name);
Attempts:
2 left
💡 Hint

Nullable types need to be checked before comparing their values.

📝 Syntax
advanced
1:30remaining
Identify the Syntax Error in LINQ Query

Which option contains a syntax error in the LINQ query that selects all customers with balance greater than 1000?

C Sharp (C#)
public class Customer {
    public string Name { get; set; }
    public decimal Balance { get; set; }
}

var customers = new List<Customer> {
    new Customer { Name = "Anna", Balance = 1200 },
    new Customer { Name = "Ben", Balance = 800 },
    new Customer { Name = "Cara", Balance = 1500 }
};
Avar richCustomers = customers.Where(c => c.Balance > 1000).Select(c.Name);
Bvar richCustomers = customers.Where(c => c.Balance > 1000).Select(c => c.Name);
Cvar richCustomers = from c in customers where c.Balance > 1000 select c.Name;
Dvar richCustomers = customers.Where(c => c.Balance > 1000).Select(c => c.Name).ToList();
Attempts:
2 left
💡 Hint

Check the syntax of the Select method usage.

🚀 Application
expert
3:00remaining
Count Unique Cities from a List of People Using LINQ

Given a list of Person objects where each has a City property, which LINQ expression correctly counts how many unique cities are in the list?

C Sharp (C#)
public class Person {
    public string Name { get; set; }
    public string City { get; set; }
}

var people = new List<Person> {
    new Person { Name = "Tom", City = "New York" },
    new Person { Name = "Sara", City = "Boston" },
    new Person { Name = "Mike", City = "New York" },
    new Person { Name = "Anna", City = "Chicago" }
};
Aint count = people.GroupBy(p => p.City).Count();
Bint count = people.Count(p => p.City.Distinct());
Cint count = people.Select(p => p.City).Distinct().Count();
Dint count = people.Select(p => p.City).Count();
Attempts:
2 left
💡 Hint

Think about how to get unique values and then count them.