"Я хочу перебрать словарь и удалить элементы на основе значения, а не ключа изначально. Затем я хочу удалить ключ после удаления всех значений в ключе."
Вот один из способов сделать это, если я правильно понимаю:
Dictionary<int, List<int>> source = new Dictionary<int, List<int>>();
source.Add(1, new List<int> { 1, 2, 3, 4, 5});
source.Add(2, new List<int> { 3, 4, 5, 6, 7});
source.Add(3, new List<int> { 6, 7, 8, 9, 10});
foreach (var key in source.Keys.ToList()) // ToList forces a copy so we're not modifying the collection
{
source[key].RemoveAll(v => v < 6); // or any other criterion
if (!source[key].Any())
{
source.Remove(key);
}
}
Console.WriteLine("Key count: " + source.Keys.Count());
foreach (var key in source.Keys)
{
Console.WriteLine("Key: " + key + " Count: " + source[key].Count());
}
Вывод:
Подсчет ключей: 2
Ключ: 2 Количество: 2
Ключ: 3 Количество: 5