Visual C# | Writing LINQ exercise | Use LINQ to iterate over Doctor Who candidate names

This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.

Software ==> Visual C#  (79 exercises)
Version ==> Any version of C#
Topic ==> Writing LINQ  (2 exercises)
Level ==> Relatively easy
Subject ==> C# training
Before you can do this exercise, you'll need to download and unzip this file (if you have any problems doing this, click here for help).

You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

In a new or existing project, add the existing form from the folder shown above.  Make this the default form, so that this is what you see when you run your application:

Next Doctor Who

Who'll be next? You can decide on the short-list.

Add code to the button so that it creates a string array of candidates (yours can be different!):

// create array of candidates

string[] candidates = { "Jeremy Corbyn", "Sooty",

"Boris Johnson", "Adam Ant",

"Simon Cowell", "Oliver Owl" };

Add LINQ code to create a query expression to enumerate these names in alphabetical order, then show these names in the listbox using code like this:

// add these to the listbox

lstCandidates.DataSource = DoctorWhoCandidates.ToList();

A listbox won't accept an IEnumerable collection as its data source, so that's why you need to cast the results to a list.  But you knew that, didn't you?

Test that when you click on the button, the names appear in the listbox!

You can unzip this file to see the answers to this exercise, although please remember this is for your personal use only.
This page has 0 threads Add post