简体   繁体   中英

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

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]'.'

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: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM