- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个数据源,其中包含 3 个不同的值,如下所示,
List<Configuration> lst = new List<Configuration>
{
new Configuration{Name="A", Config="X", Value="1"},
new Configuration{Name="A", Config="X", Value="2"},
new Configuration{Name="B", Config="Y", Value="2"}
};
public class Configuration
{
public string Name { get; set; }
public string Config { get; set; }
public string Value { get; set; }
}
在这里,我想遍历整个源,并希望将“Name”值保留为 KEY,将“Config”和“Value”保留到“NameValueCollection”中。
为此,我使用如下字典,
var config = new Dictionary<string, NameValueCollection>();
但是在添加到这本词典时我遇到了 2 个问题,
foreach(var c in lst)
{
config.Add(c.Name, new NameValueCollection { c.Config, c.Value });
}
注意 - 我想要 X 的 1 和 2(在重复键的情况下)
有没有更好的C#集合或者如何解决上面的错误。
谢谢!
最佳答案
您可以使用 lookups 的字典(它表示一个集合,其中键映射到多个值):
var config = lst.GroupBy(cfg => cfg.Name)
.ToDictionary(g => g.Key,
g => g.ToLookup(cfg => cfg.Config, cfg => cfg.Value));
配置类型将是
Dictionary<string, ILookup<string, string>>
访问值:
config["A"]["X"] // gives you IEnumerable<string> with values ["1","2"]
关于c# - 添加到 C# 字典 : cannot convert from 'string' to 'System.Collections.Specialized.NameValueCollection' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43629002/
我正在尝试构建模板类 Fod template class Fod { ... }; 它将包含一个带有 static const int value 的内部类 At指示模板参数的索引(S0 为 0,S
我很难理解 Python in a Nutshell 的最后一部分(粗体) Per-Instance Methods An instance can have instance-specific bi
我需要密码字段的正则表达式。 要求是: 密码长度必须在8到20个字符之间 必须包含至少一个字母和一个数字以及来自!@#$%^&*() 的特殊字符_+。 不应以特殊字符开头 我试过了 ^(?=.*[a-
我有一个 C 类型的输入字段。 PARAMETERS lv_sep TYPE c. 字段 lv_sep 应该只接受特殊字符。 你能帮助我如何给出这个约束吗? 最佳答案 您可以在 AT-SELECTIO
我记得 PaulP 展示了一个很酷的技巧来缩写重复的长 @specialized序列,但我找不到原来的帖子了。就像我有 trait Foo[@specialized(Int, Float, Doubl
我有一个特征和一个实现,如下所示: trait Foo[A] { def bar[B >: A: Ordering]: Foo[B] } class FooImpl[A]( val a: A, v
在Sas9中,如何用下划线替换我选择的所有,\ /或空格以及其他特殊字符?无论是在数据步骤中还是在宏函数中的解决方案都可以解决问题,我只是在寻找一种实现方法。 谢谢 最佳答案 您可以使用SAS内置的P
SPECIALIZE 的目的pragma 是创建更具体的函数版本。 我有一个功能 adaptBlocks :: Int -> BlocksField a -> Maybe (BlocksField a
我尝试使用 gcc 10 -std=gnu++20 -fconcepts 构建以下内容: template class MyClass{ T a; }; template class MyClas
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭11 年前。 Improve th
在哪里可以找到文件名中允许的字符列表(取决于操作系统)?(例如,在 Linux 上,文件名中允许使用字符 :,但在 Windows 上则不允许) 最佳答案 您应该从 Wikipedia Filenam
我有下面的powershell功能 Function Test { Param ( [Parameter()] [strin
我有下面的powershell功能 Function Test { Param ( [Parameter()] [strin
我有两个方阵 A 和 B。它们的每一项都有 1 或 0。示例如下所示 A channel id a b c 1 1 1 1 2 1 0 1 3 1 0 0 B id cha
是否可以将基础对象“特化”为派生对象? 例如: class base{... base(...) : ... {}//both have their own constructors virt
我有两个像这样连接在一起的圆圈: 我在形状内部有一个点,我想从该点沿一个方向将光线转换到形状上。为了检索形状边缘的类型转换位置。 我的第一个想法是将 2 段连接到 2 个圆圈进行光线转换。如果没有成功
在我的 Java 项目中,我有以下类/接口(interface)层次结构: public interface ProductSearcher { Set search(String reque
是否可以在不引入与实现完全匹配的签名的情况下定义这个专门的重载? on(eventName: string, cb: Function); on(eventName: "view", cb: (arg
偶然发现 def foo(f: Int => Unit) {} def foo(f: Long => Unit) {} 由于 method foo is defined twice 无法编译.我知道上
在下面的例子中,为什么 foo(f)叫暧昧? 我知道第二个重载也适用于 P == () , 但为什么第一个不被认为更专业, 因此更好的匹配? func foo(_ f: () -> R) { prin
我是一名优秀的程序员,十分优秀!