2011-09-27 53 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

如果你真的關心「循環收集」(你*測量*它真的需要太多時間),你可以優化內部存儲。 –