'Dictionary<string, List<string>> and LINQ, C#
I have a question. I would like to use LINQ to filter strings in Values (List<strings>) in my Dictionary<string, List<string>>. I want to remove string containing word "time".
How can I do in the simplest way?
EDIT:
Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>
dict.Add
{("1stList", new List("time", "water", "pc")});
dict.Add("2ndList", new List("cat", "dog", "bird)});
Dictionary looks this way simplified.
I just want to delete "time" from 1st list.
Solution 1:[1]
Try
var myDict = new Dictionary<string, List<string>>
{
{"1", new List<string>{"time", "clock"}},
{"2", new List<string>{"clock"}}
};
myDict
.Select(kvp => new KeyValuePair<string, List<string>>(
kvp.Key, kvp.Value.Where(v => !v.Contains("time")).ToList()))
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
// {"1", {"clock"}, "2", {"clock"}}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
