3 回答
Your first Select
seems redundant. You should generally filter first (using Where
) then Select
.
EDIT: Doh - I missed the new RegionInfo()
. What about:
var ccy = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
.Select(c => new RegionInfo(c.LCID))
.First(ri => ri.ISOCurrencySymbol == "ILS").CurrencySymbol;
That assumes you know you are dealing with a valid ISOCurrencySymbol
. If you can't make that assumption then use FirstOrDefault
and check the result for null
first before accessing the CurrencySymbol
.
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.
You can use overloaded First method which accepts predicate (to combine Where
and First
)
CultureInfo.GetCultures(CultureTypes.SpecificCultures)
.Select(c => new RegionInfo(c.LCID))
.First(r => r.ISOCurrencySymbol == "ILS")
.CurrencySymbol;
This code does exactly what your code does. But actually you should use FirstOrDefault
and try to get region for your ISO currency symbol. If region is found, then use it:
var isoCurrency = "ILS"
var region = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
.Select(c => new RegionInfo(c.LCID))
.FirstOrDefault(r => r.ISOCurrencySymbol == isoCurrency);
if (region != null)
// get region.CurrencySymbol
This should do it:
var t = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
.Select(d => new RegionInfo(d.LCID))
.First(f => f.ISOCurrencySymbol == "ILS").CurrencySymbol;