title: C# - Object List to Dictionary
body: <p>Is there a way to assign the key values of each object in a List to a Dictionary? I want to access that dictionary later to assign the values to properties of a class.</p>
  
  <p>I fetch some values from a BD with inner joins, then I store the values in a list of objects:</p>
  
  <pre><code>IEnumerable&lt;object&gt; units = _unitDetailsQuery.Execute&lt;object&gt;(out totalRecords);
  </code></pre>
  
  <p>Each object has these properties:</p>
  
  <ul>
  <li>Brand (string)</li>
  <li>Model (string)</li>
  <li>Code (string)</li>
  <li>BranchId (int)</li>
  </ul>
  
  <p>Then I create a Dictionary</p>
  
  <pre><code>IDictionary&lt;string, object&gt; props = new Dictionary&lt;string, object&gt;();
  </code></pre>
  
  <p>I've seen in another question something like this (i've adapted the snippet to my code):</p>
  
  <pre><code>BindingFlags bindingAttr = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance;
          foreach (object unit in units)
          {
              props = unit.GetType().GetProperties(bindingAttr).ToDictionary
                  (
                      propInfo =&gt; propInfo.Name,
                      propInfo =&gt; propInfo.GetValue(unit, null)
                  );
          }
  </code></pre>
  
  <p>But props gets no value after the assignment.</p>
  
  <p>Internally the object is something like this:</p>
  
  <p><img src="https://i.stack.imgur.com/njeQT.png" alt="screenshot of autos window"></p>
  
  <p>So how do I get those values? Thanks in advance.</p>