Back
Close

C# LINQ Introduction

breigo
42.6K views

Time for a quiz

You read a lot so far. Now it is time to test your knowledge!

We present some statements, you answer the questions.

Example 1 - Warmup

var people = new List<Person>()
{
    new Person() { Name = "Samina Stephenson", Age = 9 },
    new Person() { Name = "Jaydon Heath", Age = 82 },
    new Person() { Name = "Imaani Macgregor", Age = 66 },
    new Person() { Name = "Caiden Leonard", Age = 52 }
};

var result = people.FirstOrDefault(p => p.Age > 60 && p.Age < 70);
Name the person(s) returned in Example 1?

Example 2 - Writing LINQ

In this example you are requested to write a simple LINQ query.

You get a collection of people and shoud return all people that are at least (>=) 30 years old, sorted by name.

Use LINQ methods discussed before: Where, Take, Skip, OrderBy, OrderByDescending, ThenBy, ThenByDescending, Select, First, Last, Single, FirstOrDefault, LastOrDefault, SingleOrDefault.

Exymple 2

Example 3 - Understanding LINQ

var people = new List<Person>()
{
    new Person() { Name = "Samina Stephenson", Age = 9 },
    new Person() { Name = "Jaydon Heath", Age = 82 },
    new Person() { Name = "Imaani Macgregor", Age = 66 },
    new Person() { Name = "Caiden Leonard", Age = 52 }
};

var result = people
                .Where(x => x.Age >= 30)
                .Take(2)
                .Select(x => x.Name)
                .Last();
What is the type of the result?
Name the person(s) returned in Example 3?
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io