title: Clever regex and LINQ in C#
body: <p>I have a directory. In this directory there are lots of various files. I need only thumbnails. So I wrote a regex to match those files.</p>
  
  <p>Then, having all those matches files - I would like to make a dictionary that would have:</p>
  
  <ul>
  <li>the key as the extracted number from the filename (the matched number - see code below) </li>
  <li><p>the value as Image</p>
  
  <pre><code>    public readonly IReadOnlyDictionary&lt;int, Image&gt; thumbnails;
  
      ItemData(string directory)
      {
          this.directory = directory;
  
          var files = Directory.GetFiles(directory);
  
          Regex r = new Regex(directory + "\\thumbnail-([0-9]+).jpg", RegexOptions.RightToLeft);
  
          List&lt;int&gt; sizes = new List&lt;int&gt;();
          List&lt;Image&gt; images = new List&lt;Image&gt;();
  
          for (int i = 0; i &lt; files.Count(); i++)
          {
              string result = r.Match(files[i]).Groups[1].Value;
              if (result != "")
              {
                  sizes.Add(Int32.Parse(result));
                  images.Add(Image.FromFile(files[i]));
              }
          }
  
          thumbnails = sizes.Zip(images, (s, i) =&gt; new {s, i}).ToDictionary(item =&gt; item.s, item =&gt; item.i);
      }
  </code></pre></li>
  </ul>
  
  <p>I truly believe this could be written in a very short and clear way. Could you please help me?</p>
  
  <p><strong>Edited solution</strong></p>
  
  <pre><code>public readonly Dictionary&lt;int, Image&gt; thumbnails;
  
  public ItemData(string directory)
  {
      Regex r = new Regex("^thumbnail-([0-9]+)$", RegexOptions.RightToLeft);
  
      thumbnails =
          Directory.GetFiles(directory)
          .Select(f =&gt; new
          {
              file = f,
              match = r.Match(Path.GetFileNameWithoutExtension(f))
          })
          .Where(x =&gt; x.match.Success)
          .Select(x =&gt; new
          {
              size = Int32.Parse(x.match.Groups[1].Value),
              image = Image.FromFile(Path.Combine(directory, x.file))
          })
          .ToDictionary(x =&gt; x.size, x =&gt; x.image);
  }
  </code></pre>