Using C# LINQ - A Practical Overview

player_one
5,174 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Combined Exercise #2

Let's do one more!

In this exercise, combine LINQ method calls together to do the following:

  • Reduce the provided list of words to only those that contain the lower-case letter 'e' in them
  • Sort the resulting list by alphabetical order
  • Obtain the last word in the sorted list
  • Return the string, "The last word is <word>", using the last word from the sorted list
  • If the sorted list is empty, return null
Combined Exercise 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System.Collections.Generic;
using System.Linq;
namespace AllTogether1
{
public class FullExercise2
{
// Given a sequence of words, get rid of any that don't have the character 'e' in them,
// then sort the remaining words alphabetically, then return the following phrase using
// only the final word in the resulting sequence:
// -> "The last word is <word>"
// If there are no words with the character 'e' in them, then return null.
//
// TRY to do it all using only LINQ statements. No loops or if statements.
public static string GetTheLastWord(IEnumerable<string> words)
{
return words
// .???().???() ... .???()
;
}
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content