title: trying to replace a field name using a variable at run time in EF
body: <p>Let me preface that I have searched this question and tried a ton of things but I am obviously missing something simple (I think).</p>
  
  <p>I believe my solution will involve reflection but I just can't get it right.</p>
  
  <p><strong>Edit</strong>: 
  Quicker question - I need to replace the .fieldname of a query result with a string.  So <code>var value = stops.First().con_name;</code>  the <code>.con_name</code> needs to be dynamic at run time.</p>
  
  <p>Basics:
  I am creating the ability to generate custom reports from a database where the report layout is stored in a table of column names, column position and field names.</p>
  
  <p>So the below is the data for a very simple report:</p>
  
  <pre><code>record 1
  col_index  - 17
  field_name - con_name
  
  record 2
  col_index -  18
  field_name-  con_city
  </code></pre>
  
  <p>I am needing to query a table called <code>stop_details</code> for a collection of records that are to be included in the report.</p>
  
  <p>I then want to query a table called <code>report_matrix</code> that will return the above records (the two sample records I gave).</p>
  
  <p>I then need to step through the matrix results and create the columns based on the data.</p>
  
  <p>The challenge is replacing the field_name at run time.
  So in the below code on the first run through it would have <code>var value = stops.First().con_name;</code>  the second time .con_name would be replaced with <code>.con_city</code> etc....</p>
  
  <p>Here is my code:</p>
  
  <pre><code>     //get collection of stops for export
  
          var stops = (from s in db.stop_details 
                       where s.eCourier_export_flag == true 
                       select s)
                       .ToArray();
  
          //pull in all the report format details
  
          var reportLayout = (from r in db.report_matrix 
                              where r.report_id == 1 
                              select r)
                              .ToArray();
  
          //just do one line
  
          foreach (var layoutElement in reportLayout)
          {                
              // Select the PropertyInfo of the column.
              PropertyInfo propertyInfo = 
                stops.First().GetType().GetProperty(layoutElement.field_name);
              // name = stops.GetValue(propertyInfo, null).ToString();
              // HERE is where I need to replace .con_name with the field name from the query
              var value = stops.First().con_name; 
           }
  </code></pre>
  
  <p>Thank you in advance for any help....
  Joe</p>