2011-09-27 87 views
0

我需要使用静态缓存中的国家/地区列表保存类。C#在可访问变量中存储字符串,int,字符串

的数据是建立与

string shortName //Primary Key - (IL or UK for example) 
int ID //Unique - has no meaning, but needs to be saved 
string longName //(Israel or United Kingdom for example) 

我想将其保存在一个字典:

Dictionary<string , Dictionary<int,string>> list = new Dictionary<string, Dictionary<int,string>>(); 

这是我需要在类的API:

Countries.getByShortName();// I dont know what to return, I'd love some advise 
Countries.getById();// I might need that 
Countries.getAll();// I dont know what to return, I'd love some advise 

你认为处理这个课程的最好方法是什么?

感谢

+0

定义类型来保存你的数据,而不是使用的字典? –

+0

@JeffMercado关心提供一个例子?谢谢 – SexyMF

回答

0

我建议使用的静态方法方法:

public class Country 
{ 
    public string ShortName {get;set;} 
    public int ID {get;set;} 
    public string LongName { get; set; } 
} 

public class Countries 
{ 
    static Dictionary<string, Country> dict = new Dictionary<string, Country>(); 
    public static void Add(Country country) 
    { 
     if (!dict.ContainsKey(country.ShortName)) 
      dict.Add(country.ShortName, country); 
    } 
    public static Country GetByShortName(string ShortName) 
    { 
     if (dict.ContainsKey(ShortName)) 
      return dict[ShortName]; 
     return null; 
    } 
    public static Country GetById(int id) 
    { 
     var result = from country in dict 
        where country.Value.ID==id 
        select new Country 
        { 
         ID = country.Value.ID, 
         ShortName = country.Value.ShortName, 
         LongName = country.Value.LongName 
        }; 
     return result.SingleOrDefault(); 
    } 
    public static List<Country> GetAll() 
    { 
     var result = from country in dict 
        select new Country 
        { 
         ID = country.Value.ID, 
         ShortName = country.Value.ShortName, 
         LongName = country.Value.LongName 
        }; 
     return result.ToList(); 
    } 
} 
1

我想你需要创建自己的类incapsulating所有三个字段,并使用它的自己的收藏。例如:

class CountryInfo 
{ 
string shortName //Primary Key - (IL or UK for example) 
int ID //Unique - has no meaning, but needs to be saved 
string longName //(Israel or United Kingdom for example) 
} 

class CountryCollection : Collection <CountryInfo> 
{ 
//Implement methods what you need 
void getByShortName();// I dont know what to return, I'd love 
void getById();// I might need that 
void getAll();// I dont know what to return, I'd l 
} 

如果你想快速搜索,然后用一对词典:

class CountryInfo 
    { 
    string shortName //Primary Key - (IL or UK for example) 
    int ID //Unique - has no meaning, but needs to be saved 
    string longName //(Israel or United Kingdom for example) 
    } 

    class CountryCollection 
    { 
     Dictionary <int, string> Ids = new Dictionary <int, string>(); 
     Dictionary <string, string> shortNames = new Dictionary <string, string>(); 

    void Add (CountryInfo info) 
{ 
    Ids.Add (info.ID, info.longName); 
    shortnames.Add(info.ID, info.longName); 
} 
    //Implement methods what you need 
    void getByShortName();// I dont know what to return, I'd love 
    void getById();// I might need that 
    void getAll();// I dont know what to return, I'd l 
    } 
+0

但是,如果我需要在它上搜索getById,我需要一遍又一遍地遍历它。谢谢 – SexyMF

2

如何使用structclass

class Country 
{ 
    string shortName; // Primary Key - (IL or UK for example) 
    int ID; // Unique - has no meaning, but needs to be saved 
    string longName; // (Israel or United Kingdom for example) 
} 

然后,您可以您的国家存储在一个通用的List,例如:

List<Country> countries = new List<Country>(); 
countries.Add(new Country() 
{ 
    shortName = "UK", 
    ID = 1, 
    longName = "United Kingdom", 
}); 

实现您给出的方法,然后就变得很简单:

Country getByShortName(string shortName) 
{ 
    foreach (Country country in countries) 
    { 
     if (country.shortName == shortName) 
     { 
      return country; 
     } 
    } 
    return null; 
} 
+0

但是,如果我需要在它上搜索getById,我需要一遍又一遍地遍历它。谢谢 – SexyMF

+0

@SexyMF我认为你是过早地优化 - 做最先工作的最简单的东西 – Justin

1

你应该创建一个自定义类型:

public class Country 
{ 
    public string ShortName {get; set;} 
    public int ID {get; set;} 
    public string LongName {get; set;} 
} 

,然后存储在Dictionary<string, Country> 国家从中你就可以做到:

var UK = _countries["UK"]; 
UK.ID... 
UK.LongName... 
1

为什么不会你让你自己的类?

class Country 
{ 
    public string shortName { get; set; } //Primary Key - (IL or UK for example) 
    public int ID { get; set; } //Unique - has no meaning, but needs to be saved 
    public string longName { get; set; } //(Israel or United Kingdom for example) 
} 

然后只是让另一个类,将包含你需要

class Countries 
{ 
    List<Country> countries = new List<Country>(); 

    public void Add(Country c) 
    { 
     countries.Add(c); 
    } 

    public List<Country> getByShortName(); 
    public List<Country> getById(); 
    public List<Country> getAll(); 
} 
+0

如果你真的关心“循环收集”(你*测量*它真的需要太多时间),你可以优化内部存储。 –