title: How i can change the order of the fields and the fields names inside my CSVHelper
body: <p>I am working on an asp.net mvc 4 web application + entity framework 5.0, and i have the following method which call a function named <code>AdvanceSearchStoredProcedure</code>, where this function will call a stored procedure:-</p>
  
  <pre><code>     public IQueryable&lt;AdvanceSearchSP_Result&gt; AdvanceSearch(NetworkAdvanceSearch na)
        {
  
          var r = entities.AdvanceSearchStoredProcedure("network",na.AssetTypeID,
          na.ip, na.ipselection, na.mac, na.macselection);
          return r.AsQueryable();           
  
        }
  </code></pre>
  
  <p>then i am calling this method from my action method as follow:-</p>
  
  <pre><code>public ActionResult AdvanceSearchIndexExport(AdvanceSearchSP_Result2 ns)
  
          {
              var result = WriteCsvToMemory(repository.AdvanceSearch(ns.NetwotkAS).ToList());
              var memoryStream = new MemoryStream(result);
              return new FileStreamResult(memoryStream, "text/csv") { FileDownloadName = "export.csv" };
          }
  public byte[] WriteCsvToMemory(IEnumerable&lt;AdvanceSearchSP_Result&gt; records)
        {
            using (var memoryStream = new MemoryStream())
            using (var streamWriter = new StreamWriter(memoryStream))
            using (var csvWriter = new CsvWriter(streamWriter))
            {
                csvWriter.WriteRecords(records);
                streamWriter.Flush();
                return memoryStream.ToArray();
            }
        }
  </code></pre>
  
  <p>here is a partial class for my <code>AdvanceSearchSP_Result</code> and its MetadataType class:-</p>
  
  <pre><code>[MetadataType(typeof(AdvanceSearchSP_Result_Validation))]
  
      public partial class AdvanceSearchSP_Result
      {
  
  
      }
  public class AdvanceSearchSP_Result_Validation{
          [Display(Name="Service Tag")]
          public string SERVICETAG { get; set; }
           [Display(Name = "Serial Number")]
          public string SERIALNO { get; set; }
          [Display(Name = "Resource Tag")]
           public string Tag { get; set; }
          [Display(Name = "Resource Name")]
           public string RESOURCENAME { get; set; }
          [Display(Name = "State")]
           public string StateName { get; set; }
          [Display(Name = "Type")]
           public string TypeName { get; set; }
          [Display(Name = "Site Name")]
           public string sitename { get; set; }
          [Display(Name = "Customer Name")]
           public string cusotmername { get; set; }
           public bool ismanaged { get; set; }
  }
  </code></pre>
  
  <p>now i am facing these problems inside the generated <code>.CSV</code> file:-</p>
  
  <ol>
  <li><p>the fields title will be equal to the column names inside the database, and not as defined inside the <code>AdvanceSearchSP_Result_Validation</code> MetadataType class.</p></li>
  <li><p>second question, i am not sure how i can change the order of the columns? as currently the order will be as the one defined inside the stored procedure..for example how i can force the <code>ismanaged</code> to be the first column inside the .csv file?? </p></li>
  </ol>
  
  <p>i am using the CSVHelper <a href="https://www.nuget.org/packages/CsvHelper/" rel="nofollow noreferrer">https://www.nuget.org/packages/CsvHelper/</a> library to do the actual CSV export.</p>
  
  <p><strong>EDIT</strong>
  now i added this class:-</p>
  
  <pre><code>public sealed class SPMap : ClassMap&lt;AdvanceSearchSP_Result&gt;
      {
          public SPMap()
      {
          Map(m =&gt; m.Tag).Name("Resource Tag").Index(0);
          Map(m =&gt; m.RESOURCENAME).Name("Asset Name").Index(1);
          Map(m =&gt; m.cusotmername).Name("Customer Name").Index(2);
          Map(m =&gt; m.sitename).Name("Site Name").Index(3);
          Map(m =&gt; m.StateName).Name("Asset State").Index(4);
          Map(m =&gt; m.ismanaged).Name("ismanaged").Index(5);
          Map(m =&gt; m.TypeName).Name("Technology Type ").Index(6);
          Map(m =&gt; m.SERVICETAG).Name("Service Tag").Index(7);
          Map(m =&gt; m.SERIALNO).Name("Serial Number").Index(8);
  
      }
      }
  </code></pre>
  
  <p>then i modify my method to include the configuration as follow:-</p>
  
  <pre><code> public byte[] WriteCsvToMemory(IEnumerable&lt;AdvanceSearchSP_Result&gt; records)
        {
            using (var memoryStream = new MemoryStream())
            using (var streamWriter = new StreamWriter(memoryStream))
            using (var csvWriter = new CsvWriter(streamWriter))
            {
                csvWriter.WriteRecords(records);
                csvWriter.Configuration.RegisterClassMap&lt;SPMap&gt;();
                streamWriter.Flush();
                return memoryStream.ToArray();
            }
        }
  </code></pre>
  
  <p>but still i got the old names &amp; the old order.. so seems  the <code>public sealed class SPMap : ClassMap&lt;AdvanceSearchSP_Result&gt;</code> did not have any effect...</p>