0

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.


in addition to cd smith's very good suggestion, you can also add a hidden field if you don't want to decorate your viewmodel in this way, this would look something like:

@Html.HiddenFor(x => x.Review_UserID)

etc, etc for each FK you want to have on the form, but not be visible. Of course, this is similar to how the [ScaffoldColumn(false)] decorator works of course, tho adding the [HiddenInput(DisplayValue = false)] attribute does this explicitly for you.

4

4 に答える 4

9

Dictionary<string, City>基本的に、 の代わりに が必要なようですList<City>。これは LINQ で簡単に作成できます。

var dictionary = list.ToDictionary(city => city.Name);

本当に順序を維持したい場合は、リストを使用して検索することができますが (Ethan の回答に従って)、名前だけで検索する必要がある場合は、辞書が必要です。

于 2012-05-23T19:23:03.993 に答える
3

You can use LINQ:

List<City> theList = new List<City>();
City theCity = theList.FirstOrDefault( x => x.Name == "London" );
于 2012-05-23T19:22:55.037 に答える
3

ラッパー リスト クラス CityList を作成し、[] 演算子をオーバーロードします。

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();
    }
}

キャストを必要としない、よりスマートな方法があるかもしれません。これは .NET 2.0 コードに似ています。

于 2012-05-23T19:23:14.850 に答える
0

どうSystem.Collections.ObjectModel.KeyedCollectionですか?

http://msdn.microsoft.com/en-us/library/ms132438.aspx

于 2012-05-24T05:32:33.230 に答える