Pulling a subset of elements from a Dictionary I have a function that returns a set of values from a Dictionary.
I don't currently want to allow it to throw KeyNotFoundException, so I'm filtering the keys out before I try to select the values.

I have a feeling this can be done in a more straightforward fashion, but I'm not sure where to go with it.

// this.resources is a Dictionary&lt;string, Resource&gt;

public IEnumerable&lt;Resource&gt; GetResources(IEnumerable&lt;string&gt; resourceNames)
{
    HashSet&lt;string&gt; hs = new HashSet&lt;string&gt;(resourceNames);

    hs.IntersectWith(this.resources.Keys);

    List&lt;Resource&gt; resources = new List&lt;Resource&gt;();

    foreach(string resourceName in hs)
    {
        resources.Add(this.resources[resourceName]);
    }

    return resources;
}
I would write it the obvious straight-forward LINQ way, which gives you good readability as well as lazy evaluation:

public IEnumerable&lt;Resource&gt; GetResources(IEnumerable&lt;string&gt; resourceNames)
{
    return resourceNames
        .Where(name =&gt; resources.ContainsKey(name))
        .Select(name =&gt; resources[name]);
}