Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CultureInfo from RegionInfo using LINQ

Need some help as this is driving me insane and I can't tell if I'm doing something stupid or not.

I'm trying to gather a list of all specific cultures and get their corresponding RegionInfo. from that list I'm attempting to get the region corresponding to a country supplied by a parameter. I'm attempting this through LINQ, as seen below;

public RegionInfo getregion(string country)
{
    RegionInfo thisregion;
    List<RegionInfo> regions = new List<RegionInfo>();
    CultureInfo[] allregions = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
    regions.AddRange(allregions.Where(x => !x.IsNeutralCulture).Select(x => new RegionInfo(x.LCID)));
    thisregion = regions.FirstOrDefault(region => region.EnglishName == country);
    return thisregion;
}

This particular function keeps throwing an exception during the Select() statement, error below;

Additional information: Culture ID 4096 (0x1000) is a neutral culture; a region cannot be created from it.

The culture in question causing the issue is aa-ER. As you can see, I have filtered the list based on the IsNeutralCulture Boolean, so the net framework doesn't think this culture is neutral, yet the error says otherwise.

Am I doing something stupid?

EDIT: Interestingly, the first comment lead me to test it on a different machine, which didn't throw the error on the same culture. Looks like a machine problem.

like image 968
Ryan Hargreaves Avatar asked Aug 17 '16 12:08

Ryan Hargreaves


1 Answers

OK, so as a workaround to this issue, I changed the Select statement to initialise RegionInfo for x.Name instead of x.LCID and this has worked. Don't know why the LCID was failing, but this solution will do.

like image 152
Ryan Hargreaves Avatar answered Sep 28 '22 10:09

Ryan Hargreaves