【发布时间】:2019-11-27 10:33:40
【问题描述】:
我确实有一个Culture 列表和一个翻译列表:
public class Translation
{
public string key { get; set; }
public string value { get; set; }
public string cultureId { get; set; }
}
public async Task<IActionResult> ReactGetResources(string module = "shared")
{
string[] Languages = { "en-US", "fr-FR", "it-IT", "es-ES" };
List<Translation> Translation = new List<Translation>();
Translation.Add(new Translation { key = "hello", value = "Hello", cultureId = "en-US" });
Translation.Add(new Translation { key = "hello", value = "Bonjour", cultureId = "fr-FR" });
Translation.Add(new Translation { key = "hello", value = "Buongiorno", cultureId = "it-IT" });
Translation.Add(new Translation { key = "goodbye", value = "Good Bye", cultureId = "en-US" });
Translation.Add(new Translation { key = "goodbye", value = "Au revoir", cultureId = "fr-FR" });
Translation.Add(new Translation { key = "goodbye", value = "adiós", cultureId = "es-ES" });
Dictionary<string, List<string>> query = Translation
.OrderBy(o => o.cultureId)
.GroupBy(o => o.key)
.ToDictionary(g => g.Key, g => g.Select(x => x.value).ToList());
return Json(query);
}
当前代码IActionResult返回:
{
"hello": [
"Hello", //en
"Bonjour", //fr
"Buongiorno" //it
],
"goodbye": [
"Good Bye", //en
"Au revoir", //fr
"Adiós", //es
]
}
但我希望为缺少的翻译添加 Null 值:
{ "en-US", "fr-FR", "it-IT", "es-ES" }
{
"hello": [
"Hello", //en
"Bonjour", //en
"Buongiorno", //it
null //es
],
"goodbye": [
"Good Bye", //en
"Au revoir", //fr
null, //it
"Adiós" //es
]
}
可以只用Linq吗?或者也许我应该在字典里写一个loop 并重新填充山谷?
【问题讨论】:
-
多个翻译实例可以共享同一个
key?我还以为是PK,还是和DB无关? -
另一方面,您可以通过使用左连接来连接两个集合来完成您想要的操作。 Google 如何使用 LINQ 进行左连接。
-
是的,它不是
PK,要获得特定文化的特定翻译,我在key和cultureId上搜索
标签: c# json linq dictionary asp.net-core