gpt4 book ai didi

c# - 使用键列表初始化新字典

转载 作者:太空宇宙 更新时间:2023-11-03 18:51:42 26 4
gpt4 key购买 nike

我正在初始化一个新的字典并像这样一个一个地放置每个键:

igData = data.Select(x => new Dictionary<string, string> {
["Date"] = x.GetValueOrDefault("DATE"),
["SP"] = x.GetValueOrDefault("SP"),
["IG"] = x.GetValueOrDefault("IG")}).ToList();

这很好,但其余键需要是我在 List<string> headers 中创建的 header

是否可以在上面的初始化中添加这个标题列表?像这样:

igData = data.Select(x => new Dictionary<string, string> {
["Date"] = x.GetValueOrDefault("DATE"),
["SP"] = x.GetValueOrDefault("SP"),
["IG"] = x.GetValueOrDefault("IG"),
headers.Select(y => new KeyValuePair<string, string>
(y, x.GetValueOrDefault["IG"])).ToList();

我知道值是相同的,但这是有意的。以上是否可能,或者这是我会分开的东西。在上面这样做是理想的,因为我可以访问 data 中的值。收藏。

最佳答案

你不能在这样的初始化器中做到这一点,但我建议你可以用一点点 ConcatToDictionary 来做到这一点:

igData = data.Select(x => {
return new KeyValuePair<string,string>[]{
new KeyValuePair<string,string>("Date", x.GetValueOrDefault("DATE")),
new KeyValuePair<string,string>("SP", x.GetValueOrDefault("SP")),
new KeyValuePair<string,string>("IG", x.GetValueOrDefault("IG"))
}.Concat(
headers.GetHalfHourlyTimeHeaders().Select(y => new KeyValuePair<string, string>
(y, x.GetValueOrDefault["IG"])).ToList()
).ToDictionary(k => k.Key, v => v.Value);
});

关于c# - 使用键列表初始化新字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56628894/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com