List<String> to List<CultureInfo> in asp net core

227 Views Asked by At

I want to set list cultureinfo in startup from database with query , please help me , how can i do that?

My Query

public IEnumerable<string> GetListLanguagesTypes()
    {
    var cul = (from n in _context.Language
                       select n.TypeLanguage).ToList();
    return cul ;
    }

My Code in Startup

var supportCulture = new List<CultureInfo>()
        {
            new CultureInfo("fa-IR"),
            new CultureInfo("en-US"),
            new CultureInfo("ru-RU"),
            new CultureInfo("ar-SA")
        };

i want to replace supportCulture from query

List<CultureInfo> supportCulture = (List<CultureInfo>)_language.GetListLanguagesTypes();

but i have a error

Unable to cast object of type 'System.Collections.Generic.List`1[System.String]' to type 'System.Collections.Generic.List`1[System.Globalization.CultureInfo]'.'
1

There are 1 best solutions below

0
On

You can try to use the following code:

List<CultureInfo> supportCulture =_language.GetListLanguagesTypes()
                                  .Select(x => new CultureInfo(x))
                                  .ToList();

Here is a demo:

action:

public void TestCultureInfo()
        {
            List<string> l = new List<string> { "fa-IR", "en-US", "ru-RU", "ar-SA" };
            List<CultureInfo> supportCulture = l
             .Select(x => new CultureInfo(x))
             .ToList();
        }

result: enter image description here