我有一组结果需要转换为不同的格式。结果来自Dictionary<string, List<Tuple<string, string>>>
资源。关键是语言文化LCID,元组是中性文本和本地化文本。
例如,给出的结果是:
1. 3084, [0]: Female : Femme, [1] Man : Homme, [2] Other : Autre
2. 1033, [0]: Female : Women, [1] Man : Man, [2] Other : Other
所以到现在为止我需要按照中性文本按组转换这些数据。最后一组值需要适合这个类:
public class StructuredLocalized
{
public string NeutralText { get; set; }
public Dictionary<string, string> LocalizedTexts { get; set; }
}
最终结果应该是这样的:
1. Female, [0]: 3084 : Femme, [1]: 1033: Women
2. Man, [0]: 3084 : Homme, [1] : 1033 : Man
3. Other, [0]: 3084 : Autre, [1]: 1033 : Autre
我无法编写group by子句来实现该格式化。有人可以帮忙吗?
答案 0 :(得分:1)
尝试
var tmp = (from r in x select (from v in r.Value select new { r.Key, v.Item1, v.Item2}))
.SelectMany(a=>a).ToLookup(a=>a.Item1 , a => new { a.Key, a.Item2});
var results = (from r in tmp select new
StructuredLocalized{
NeutralText = r.Key ,
LocalizedTexts = r.ToDictionary(a=>a.Key, a=>a.Item2)
} ) ;
int line = 1;
foreach(var r in results)
{
Console.Write ("{0}. {1}," , line++, r.NeutralText);
int j = 0;
foreach(var k in r.LocalizedTexts)
{
Console.Write(" [{0}]: {1} {2}, " , j++, k.Key, k.Value);
}
Console.WriteLine();
}
答案 1 :(得分:0)
我相信这就是你要找的东西。诀窍是在分组之前将字典展平为包含3个值的匿名对象。但如果可以的话,我建议您创建特定于您的域的类型。
var idToNames = new Dictionary<string, List<Tuple<string, string>>>
{
{
"3084",
new List<Tuple<string, string>>
{
Tuple.Create("Female", "Femme"),
Tuple.Create("Male", "Homme"),
Tuple.Create("Other", "Autre"),
}
},
{
"1033",
new List<Tuple<string, string>>
{
Tuple.Create("Female", "Woman"),
Tuple.Create("Male", "Man"),
Tuple.Create("Other", "Other"),
}
}
};
var nameToSpecific =
idToNames.SelectMany(
kvp => kvp.Value.Select(
t => new
{
LCID = kvp.Key,
GenericName = t.Item1,
SpecificName = t.Item2
}))
.GroupBy(x => x.GenericName)
.ToDictionary(
g => g.Key,
g => g.Select(
x => Tuple.Create(x.LCID, x.SpecificName)).ToList());
foreach (var kvp in nameToSpecific)
{
Console.WriteLine(kvp.Key);
foreach(var tup in kvp.Value)
Console.WriteLine(" " + tup);
}
结果
Female
(3084, Femme)
(1033, Woman)
Male
(3084, Homme)
(1033, Man)
Other
(3084, Autre)
(1033, Other)