I have a list of strings, and in this list of strings there might be references to a list of other strings. For instance, suppose the list is like so: [a.txt, b.txt, c.more]
, and when I iterate over the list I'd like to lookup in a dictionary: {{'c.more', [c.txt, d.txt]}}
so that the resulting list is [a.txt, b.txt, c.txt, d.txt]
as a result of looking up c.more
in the dictionary.
What I have at this point is something like this:
var dict = new Dictionary<string,List<string>>
{
{"c.more", new List<string> { "c.txt", "d.txt" } }
}
list.SelectMany(
f =>
f.EndsWith(".more")
? Expand(f)
: Include(f, dict))
Where Expand and Include do this:
public IEnumerable<string> Include(string f) { yield return f; }
public IEnumerable<string> Expand(string f, Dictionary<string,List<string>> dict) {
return dict.ContainsKey(f) ? dict[f] : new List<string>();
}
I could simply return a new List<string> { f }
in the first half of the ternary and the result of doing the lookup in the second half, but I want to later handle a recursive lookup so I'm farming out the Expand. Right now I'm not really concerned about memory usage, but I felt like there might be some other way of doing what I'm after that I haven't seen yet.
Is there a better approach to expand a list with more lists?