I was asked to find what currency symbol does USD (etc) has. ($)
another example :
ILS => ₪
So I wrote this :
var isoCurrency = "ILS".ToUpper().Trim();
foreach(CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{
RegionInfo myRI1 = new RegionInfo(ci.LCID);
if(myRI1.ISOCurrencySymbol == isoCurrency)
{
NumberFormatInfo nfi = ci.NumberFormat;
console.Write( nfi);
}
}
But it looks ugly.
So I converted it to linq :
var t= CultureInfo.GetCultures(CultureTypes.SpecificCultures)
.Select(d=>new RegionInfo(d.LCID))
.Where (f=>f.ISOCurrencySymbol=="ILS" )
.Select(f=>f.CurrencySymbol).First();
But I believe I could make it shorter. ( 2 select’s???)
any help ? (assuming this is the correct solution…)
Your first
Selectseems redundant. You should generally filter first (usingWhere) thenSelect.EDIT: Doh – I missed the
new RegionInfo(). What about:That assumes you know you are dealing with a valid
ISOCurrencySymbol. If you can’t make that assumption then useFirstOrDefaultand check the result fornullfirst before accessing theCurrencySymbol.One additional point to note is that if you are performing this query frequently you could cache the result of
CultureInfo.GetCultures(CultureTypes.SpecificCultures)), although this really is a micro optimisation.