What is the output of the following C# code using LINQ on a list of custom Person objects?
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); } } }
Look at the filter condition Age > 28 and then the ordering by Name.
The code filters people older than 28: Alice (30) and Charlie (35). Then it orders them by name alphabetically: Alice, then Charlie. So the output is those two names in that order.
Given a list of Product objects, which LINQ query correctly projects only the product names into a list of strings?
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 } };
Projection means selecting a specific property from each object.
Select is used to transform each object into something else. Here, p => p.Name picks the name property. Where filters, so option A is wrong. Option A selects prices, not names. Option A orders but does not project names.
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?
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); } } }
Nullable types need to be checked before comparing their values.
Option B checks if Age has a value before comparing, preventing a InvalidOperationException. Option B causes runtime error because it compares nullable without check. Option B throws if Age is null. Option B filters non-null but does not check age > 40.
Which option contains a syntax error in the LINQ query that selects all customers with balance greater than 1000?
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 } };
Check the syntax of the Select method usage.
Option A uses Select(c.Name) which is invalid syntax. The Select method requires a lambda expression like c => c.Name. Options B, C, and D are syntactically correct.
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?
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" } };
Think about how to get unique values and then count them.
Option C selects all cities, removes duplicates with Distinct(), then counts them. Option C is invalid because p.City.Distinct() is invalid on string like that. Option C groups by city and counts groups, which also works but is not the correct answer here because it is not the option asked for. Option C counts all cities including duplicates.