简体   繁体   English

如果包含相同的键,则从另一个数组替换值

[英]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.

相关问题 从包含另一个数组的值的数组中返回所有值 - Return all values from an array that contains the value from another array 如何使用相同的键但累积值从另一个字典填充字典 - How to populate a dictionary from another with same keys but values cumulated 检查字符串是否包含字典键并将匹配的子字符串替换为字典中的值 - check if string contains dictionary keys and replace the matching subtring with Values from dictionary 检查一个数组是否包含比相同索引处的另一个数组大的所有值 - Check if an array contains all the values greater than another array at the same index 检查一个数组是否包含另一个数组的值 - check if an array contains values of another array 如果table1包含fk到table2的同一列的多个字段,如何从一个表创建多个外键到另一个表 - How to create multiple foreign keys from one table to another if table1 contains multiple fields with fk to the same column of table2 用数组中的值替换子字符串 - Replace substrings with values from array 从包含列表并按值过滤的字典中获取键(对象) - Get keys (objects) from a dictionary that contains list and filtered by values 使用数据库中的键和值创建数组 - Create an Array with Keys and Values from database 如何用另一个数组中的值替换索引 - How to replace the indices with the value from another array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM