Using C# LINQ - A Practical Overview


Combined Exercise #1

As we have already seen in some of the examples, LINQ methods can build on each other. Since many LINQ methods return an IEnumerable<T>, subsequent LINQ methods can be called on the results. For example:

IEnumerable<string> values = new List<string> { "fe", "fi", "fo", "fum" };
// Will return 12
int result = values
    .Select(word => $"{word}-{word}") // { "fe-fe", "fi-fi", ... }
    .Select(phrase => phrase.Length)

In this exercise, combine LINQ method calls together to determine if the second sequence passed into the TestForSquares() method contains the squares of the elements in the first sequence. It should return true if the two sequences have the same number of elements and each element in squares is equal to the square of the corresponding element in numbers.

Combined Exercise 1
Create your playground on
This playground was created on, our hands-on, knowledge-sharing platform for developers.
Go to