Dictionary<string, List<string>> list1= new Dictionary<string, List<string>>();
Dictionary<string, List<string>> list2= new Dictionary<string, List<string>>();
我想根据条件将list1的项目复制到list2中:
foreach (var confirmNumber in list1.Keys)
{
if (condition)
{
list2.Add(confirmNumber,list1[confirmNumber]);
}
}
它实际上不会起作用。
答案 0 :(得分:0)
所以你只想从符合给定条件的列表中获取项目吗?
Dictionary<string, List<string>> list2 = list1
.SelectMany(kv => kv.Value.Select(s => new { kv.Key, s }))
.Where(x => condition) // you have the key and the string to apply the condition
.GroupBy(x => x.Key)
.ToDictionary(g => g.Key, g => g.Select(x => x.s).ToList());