[英]Replace Values from another array if contains same keys
I have the following collection 我有以下收藏
var collection = new []
{
"First1",
"Second2",
"Third12"
}
Then I have a lookup table, which contains keys that needs to be replaced: 然后我有一个查找表,其中包含需要替换的键:
var keys = new []
{
Tuple.Create(1, "ABC"),
Tuple.Create(2, "DEF")
}
The final result should be the original list with the values replaced from the lookup table 最终结果应该是原始列表,其中的值从查找表中替换
var collection = new []
{
"FirstABC",
"SecondDEF",
"ThirdABCDEF"
}
I was trying with the aggregate function in the following way: 我正在尝试使用聚合函数,方法如下:
string[] collection = new[]{"First#", "Second@"};
Console.WriteLine("Before {0}", String.Join(" - ", collection));
var keys = new[]{Tuple.Create("#", "ABC"),Tuple.Create("@", "CDE")};
foreach ( var item in collection)
{
if() // my item is in any occurrence of the keys array
}
Console.WriteLine("After {0}", String.Join(" - ", collection));
for (var i = 0; i < collection.Length; i++)
{
foreach (var k in keys)
{
collection[i] = collection[i].Replace(k.Item1, k.Item2);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.