Server Intellect Cloud Hosting

Fun with C# Lists (Part 2 of 3)

Category: .NET Framework

Fun with C# Lists… both of them. Part Two of Three

This article is Part Two in the series. Read Part 1 here

Introduction

In this part of the article we will be continuing to work with various methods within the List<Type> class.  We will continue to show you how easy it is to work with Lists and how they can be utilized instead of arrays.
We left off after we had populated the List with a set of Automobile Models and the seating capacity associated with each Model.    We created the Automobile class and we showed how you would use the Sort method to sort the List of Models in ascending order.

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.


Implementation

In this part of the implementation we will be working with the seats attribute from the “Automobile” class.

 

 

  • Seats

The Seats attribute denotes the seating capacity of the vehicle and the Model attribute denotes the Model of the Automobile.  These two attributes make up the two values we will be working with when sorting and searching through our List.

 

Sort the List by seating Capacity

What we are trying to accomplish here is to be able to sort the List by the seating capacity of the Automobile.  This is a numeric sort using the value of each attribute.  We are using the built in “Sort” method of the List holding all of our Models.

auto.Sort(delegate(Automobile auto1, Automobile auto2) { return auto1.seats.CompareTo(auto2.seats); });

Please see Fig. 2 for the results of the seating capacity search.  If you look at the differences between the results in Fig. 1 and in Fig. 2 you will see that the sort order changed and that the Automobile with the least number of available seating is first in the List.
In the next example we will retrieve all of the Automobiles that have a seating capacity less than 5 and sort the results by the seating capacity of the Automobile.

Display of the results of the seating capacity sort [Fig. 2]:

Image 1
[Click to see full-size]

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!


Sort and display all of the Automobiles that have less than 5 available seats

What we are trying to accomplish here is to be able to sort the List by the seating capacity of the Automobiles that offer available seating of less than 5 individuals.  You will see that we are returning a new List<Automobile> in the “results” List and no longer using the “auto” List for this result set.

 

List<Automobile> results = auto.FindAll(delegate(Automobile auto1) { return auto1.seats < 5; });

Please see Fig. 3 for the results of the seating capacity search of vehicles that allow less than 5 available seats.  If you look at the differences between the results in Fig. 2 and in Fig. 3 you will see that the sort order changed and that the Automobile List itself has also changed.  The only Automobiles in the List are the ones that have a seating capacity of less than 5.
Because we are using a new List<type> that was derived from the result set of our “FindAll” method call we are not changing the original List<Type> that holds all of our Models and it stays intact.

Display of the results of the seating capacity sort [Fig. 3]:

Image 2
[Click to see full-size]

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.


Next Up

In the next part of this series will get into how we display the results of or manipulation in a List Box and show you the finished source code so you can see how everything has tied together to complete this example.

What have we learned?

 

  • Sort the List by seating capacity
  • Sort and Display the List of Automobiles with less than 5 available seats

Continue Reading the Final Part

Attachments



Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!