Different Ways Of Retrieving Data From Collections
C# as a language has matured over years. We have come a long way from the first version of C# to the current which incorporates LINQ. Here is an example I recently used in a training session. My objective was to show students how we can retrieve items from a collection based on a certain criteria in different ways. The collection in question here is a collection of cities. City class looks like this:
public class City
{
public string Name { get; set; }
public string Country { get; set; }
}
Cities collection is initialised using this code:
List<City> cities =
new List<City>
{
new City{ Name = "Sydney", Country = "Australia" },
new City{ Name = "New York", Country = "USA" },
new City{ Name = "Paris", Country = "France" },
new City{ Name = "Milan", Country = "Spain" },
new City{ Name = "Melbourne", Country = "Australia" },
new City{ Name = "Auckland", Country = "New Zealand" },
new City{ Name = "Tokyo", Country = "Japan" },
new City{ Name = "New Delhi", Country = "India" },
new City{ Name = "Hobart", Country = "Australia" }
};
The objective is to find all cities in Australia and write their name to console. Here are the different ways it can be done.
Old Fashioned
foreach (City item in cities)
{
if (item.Country == "Australia")
Console.WriteLine(item.Name);
}
Using FindAll extension method
foreach (City item in cities.FindAll(IsAustralianCity))
{
Console.WriteLine(item.Name);
}
private static bool IsAustralianCity(City c)
{
if (c.Country == "Australia")
return true;
else
return false;
}
using FindAll with anonymous method
foreach (City item in cities.FindAll(
delegate(City c)
{
return c.Country == "Australia";
}))
{
Console.WriteLine(item.Name);
}
Using a LINQ query
var collection =
from c in cities
where c.Country == "Australia"
select c;
foreach (City item in collection)
{
Console.WriteLine(item.Name);
}
One liner using Lambda. My absolute favourite.
cities.FindAll(c => c.Country == "Australia")
.ForEach(c => Console.WriteLine(c.Name));
Quite interesting to see how the language has evolved over time. I guess this is the kind of stuff which keeps us developers going. Good on you Microsoft! Keep it coming.
One Response to Different Ways Of Retrieving Data From Collections
Leave a Reply Cancel reply
Top Posts
- LINQ To SQL Tutorial
- LINQ To SQL Join On Multiple Conditions
- Code Sample: Programmatically Download File Using C#
- Windows 7 Control Panel In Classic Mode
- More Details Emerge On Microsoft Master Certification
- Use SqlConnection With LINQ To SQL
- Free Icons And Images With Visual Studio 2008
- Capture XML In WCF Service
- Dynamic Sort With LINQ
- StyleCop Tutorial
Tags
.Net 2010 ADO.NET ASP.NET Azure Blogging Books Browsers C# Certification Cloud Computing Code Snippets Community Data Services Eclipse Entity Framework Google IDE Java LINQ Mac Microsoft Museum NetBeans Office Oracle REST SharePoint Silverlight SQL Server T-SQL Tips Tools Training Visual Studio Visual Studio 2010 WCF Web Windows Windows 7 Windows Forms Windows Live WMI WPF XAML


Elegant example, thanks for publishing it.
Personally, I like the relationship between problem and implementation to be 1 to 1! Some of us have to code (and read other’s code) to pay for rent!
Regards,
EK