c# - Sort a dictionary of Currencies into groups with LINQ -


i have dictionary of currencies:

dictionary<string, string>         _currencies = cultureinfo.getcultures(culturetypes.specificcultures)                         .select(c => new regioninfo(c.lcid))                         .where(ri => ri != null)                         .groupby(ri => ri.isocurrencysymbol)                         .todictionary(x => x.key, x => x.first().currencyenglishname); 

i want sort these group of popular currencies , rest.

so far doing this, don't it:

list<string> popularcurrencies = new list<string> {     "gbp", "eur", "usd", "aud", "cny", "inr", "sgd" };  list<currency> popular = _currencies     .where(kvp => popularcurrencies.contains(kvp.key))     .select(kvp => new currency     {         id = kvp.key,         name = kvp.key + " - " + kvp.value,         category = "popular"     })     .tolist();  list<currency> other = _currencies     .where(kvp => !popularcurrencies.contains(kvp.key))     .select(kvp => new currency     {         id = kvp.key,         name = kvp.key + " - " + kvp.value,         category = "all"     })     .tolist();  list<currency> = popular.concat(other).tolist();  public class currency  {   public string id { get; set; }   public string name { get; set; }   public string category { get; set; } } 

i thinking 2 linq queries , concat 1 line.

update:

i added ordering popular currencies. wondering if there easy way sort rest of them.

                dictionary<string, int> popularcurrencies = new dictionary<string, int>() {                 {"gbp", 1},{"eur", 2},{"usd", 3},{"aud", 4},{"cny", 5},{"inr", 6},{"sgd", 7}             };                 var = _currencies.select(kvp => new currency             {                 id = kvp.key,                 name = kvp.key + " - " + kvp.value,                 category = popularcurrencies.any(c => c.key == kvp.key) ? "popular" : "all"             }).orderbydescending(c => c.category).orderby(c => popularcurrencies.containskey(c.id) ? popularcurrencies[c.id] : int.maxvalue).tolist(); 

you can try this:

var = _currencies.select(kvp => new currency {     id = kvp.key,     name = kvp.key + " - " + kvp.value,     category = popularcurrencies.any(c => c == kvp.key) ? "popular" : "all"     }).tolist() 

added: if want popular currencies first can add orderbydescending:

var = _currencies.select(kvp => new currency {     id = kvp.key,     name = kvp.key + " - " + kvp.value,     category = popularcurrencies.any(c => c == kvp.key) ? "popular" : "all"     }).orderbydescending(c => c.category).tolist() 

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -