views:

53

answers:

1

How can I write this code more cleanly/concisely?

    /// <summary>
    /// Creates a set of valid URIs.
    /// </summary>
    /// <param name="levelVariantURIDicts">A collection of dictionaries of the form:
    ///                                     dict["filePath"] == theFilePath </param>
    /// <returns></returns>
    private ICollection<string> URIsOfDicts(ICollection<IDictionary<string, string>> levelVariantURIDicts)
    {
        ICollection<string> result = new HashSet<string>();
        foreach (IDictionary<string, string> dict in levelVariantURIDicts)
        {
            result.Add(dict["filePath"]);
        }
        return result;
    }
+10  A: 

You can select dict["filePath"] for each dict in levelVariantURIDicts with Select:

return levelVariantURIDicts.Select(dict => dict["filePath"])
                           .Distinct()
                           .ToList();

Drop .Distinct() if duplicate entries in the result are fine.
Drop .ToList() if you don't really need to return an ICollection<T> and an IEnumerable<T> is fine.

dtb
That is glorious. Thank you so much.
Rosarch