Reputation:
I have a class that contains a List<>
of objects. For this example I will say the objects are a basic class as below:
public class City
{
private string name;
private string country;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
}
Normally I would refer to these objects like so:
List<City> theList = new List<City>();
City theCity = theList[0];
What I would like to do is refer to the list as follows:
List<City> theList = new List<City>();
City theCity = theList["London"];
Where London is the name property of one of the cities.
How do I achieve this? At present I have been constructing "find" type methods that return me the city in question. What I need is to be able to refer by Key.
Upvotes: 0
Views: 306
Reputation: 43056
How about System.Collections.ObjectModel.KeyedCollection
?
http://msdn.microsoft.com/en-us/library/ms132438.aspx
Upvotes: 0
Reputation: 14564
You write a wrapper List class CityList and overload the [] operator.
public class City
{
private string name;
private string country;
public City(string cityName)
{
Name = cityName;
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
}
public class CityList : CollectionBase
{
public CityList() : base ()
{
}
public City this[int index]
{
get
{
return (City)List[index];
}
set
{
List[index] = value;
}
}
public City this[string name]
{
get
{
int index = this.IndexOf(name);
if (index < 0 || index >= this.List.Count) return null; // or assert
return (City)List[index];
}
set
{
int index = this.IndexOf(name);
if (index > 0 || index >= this.List.Count) return; // or assert
List[index] = value;
}
}
public virtual int IndexOf(City city)
{
return List.IndexOf(city);
}
public virtual int IndexOf(string name)
{
if (name == null) return -1;
for (int i = 0; i < List.Count; i++)
{
if (((City)List[i]).Name.ToLower() == name.ToLower())
return i;
}
return -1;
}
public virtual void Insert(int index, City city)
{
List.Insert(index, city);
}
public virtual int Add(City city)
{
return base.List.Add(city);
}
}
class Program
{
static void Main()
{
City NewYork = new City("New York");
City Boston = new City("Boston");
City Tampa = new City("Tampa");
CityList cities = new CityList();
cities.Add(NewYork);
cities.Add(Boston);
cities.Add(Tampa);
Console.WriteLine(cities["Boston"].Name); // prints "Boston"
Console.ReadKey();
}
}
There might be a slicker way to do it now so you don't need the casting. This is like .NET 2.0 code.
Upvotes: 3
Reputation: 1502476
Basically, it sounds like you want a Dictionary<string, City>
instead of a List<City>
. You can easily create this with LINQ:
var dictionary = list.ToDictionary(city => city.Name);
You can use a list and search through it (as per Ethan's answer) if you really want to preserve the order, but if you only need to look up by name, then a dictionary is what you want.
Upvotes: 9
Reputation: 27282
You can use LINQ:
List<City> theList = new List<City>();
City theCity = theList.FirstOrDefault( x => x.Name == "London" );
Upvotes: 3