First and Single methods to find books.FirstOrDefault and SingleOrDefault methods to handle cases with no matches.Jump into concepts and practice - no test required
First and Single methods to find books.FirstOrDefault and SingleOrDefault methods to handle cases with no matches.List<string> called books with these exact titles: "C# Basics", "LINQ Fundamentals", "C# Basics", "Advanced C#".Use new List<string> { ... } to create the list with the exact titles.
searchKeyword and set it to "C# Basics".Declare a string variable with the exact name and value.
First to find the first book in books that equals searchKeyword and store it in firstBook. Then use Single to find the single book in books that equals "LINQ Fundamentals" and store it in singleBook.Use books.First(book => book == searchKeyword) and books.Single(book => book == "LINQ Fundamentals").
FirstOrDefault to find a book matching "Nonexistent Book" and store it in firstOrDefaultBook. Use SingleOrDefault to find a book matching "Advanced C#" and store it in singleOrDefaultBook. Then print all four variables: firstBook, singleBook, firstOrDefaultBook, and singleOrDefaultBook.Use FirstOrDefault and SingleOrDefault with the exact search strings. Use Console.WriteLine to print each result. Use ?? "null" to show "null" if the result is empty.
Which method will throw an exception if the collection does not have exactly one matching element?
First(), FirstOrDefault(), Single(), SingleOrDefault()Which of the following is the correct syntax to get the first element or default from a list numbers?
var result = numbers._____();
What will be the output of this code?
var list = new List<int> { 5, 10, 15 };
var result = list.SingleOrDefault(x => x == 10);
Console.WriteLine(result);Identify the error in this code snippet:
var items = new List<string> { "apple", "banana", "apple" };
var singleItem = items.Single(x => x == "apple");
Console.WriteLine(singleItem);You have a list of users and want to get the only user with the username "admin" or null if none exists. Which method should you use to avoid exceptions if there are no or multiple admins?