Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 832863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:25:44+00:00 2026-05-15T04:25:44+00:00

Using FileHelpers , I decorated a class with [DelimitedRecord(",")] and was going to output

  • 0

Using FileHelpers, I decorated a class with [DelimitedRecord(",")] and was going to output an enumeration of objects of that type as CSV. But, it didn’t work because my class inherits from ActiveRecordLinqBase<T>, which caused some problems.

So, I was wondering if I could just select an enumeration of anonymous types and somehow have filehelpers generate csv from that. I don’t like having to define a class just for FileHelpers to output csv.

I would be open to using another csv library, but FileHelpers is proven.

EDIT

@Foovanadil: This would be the sort of thing I am trying to do:

CreateCSV(MyCollection.Select(x=>new{
    x.Prop1,
    x.Prop2,
    x.Prop3
}));

Gives you:

Prop1,Prop2,Prop3
val1a,val2a,val3a
val1b,val2b,val3b
etc.....
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T04:25:45+00:00Added an answer on May 15, 2026 at 4:25 am

    LINQ To CSV worked great for me.

    Here’s an example of how I am using it:

    protected void Page_Load(object sender, EventArgs e)
    {
        var courseId = int.Parse(Request["id"]);
        var course = Course.Queryable.Single(x => x.Id == courseId);
        Response.ContentType = "text/csv";
        Response.AddHeader("Content-Disposition", string.Format("attachment;filename=\"{0}.csv\";", course.Code));
    
        var csvContext = new LINQtoCSV.CsvContext();
        var writer = new System.IO.StreamWriter(Response.OutputStream);
        csvContext.Write(course.Registrations.Select(x => new
        {
            x.StudentId,
            x.Name,
            x.EmailAddress,
            x.MoodleUsername,
            x.Age,
            x.Is65OrOlder,
            x.CertificationAndRank,
            x.Citizenship,
            x.DateOfBirth,
            x.DepartmentName,
            x.StationNumber,        
            x.EmploymentStatus,
            x.HighestEducationLevel
        }), writer);        
    
        writer.Dispose();
    }
    

    UPDATE

    There are some downsides to the approach above:

    • The column order in the csv file is unpredictable. It doesn’t follow the order of the property definitions in the anonymous type.
    • Column headers come from the property names which aren’t always what is is desired.

    So, I decided I would create a class just for the CSV records, which didn’t end up being any more work than the anonymous type did. I used Auto Mapper to flatten my source class and populate the property values of the CSV class. I also decided to compare FileHelpers to Linq To CSV. Linq To CSV was the obvious winner, in my situation:

    • L2CSV allowed you apply an attribute to each property in the class for defining the column order, column header title, and conversion formats.
    • FHs would only let you supply a conversion format for each field. The column order depended on the order of the properties as they are defined in the class.
    • FHs would not infer a column header from the property name nor let you supply one. You could supply a literal string as the header for the CSV file, which is no good: The delimiter is built into the literal string; the column titles’ order is not synced with the order of the properties.

    I hope these findings are useful. Here is my new code:

    // CSV Class
    
    public class CsvRegistration
    {
        [CsvColumn(FieldIndex = 0)]
        public string Name { get; set; }
    
        [CsvColumn(FieldIndex = 1, Name = "Student Id")]
        public int StudentId { get; set; }
    
        [CsvColumn(FieldIndex = 2, Name = "Email Address")]
        public string EmailAddress { get; set; }
    
        [CsvColumn(FieldIndex = 3, Name = "Moodle Username")]
        public string MoodleUsername { get; set; }
    
        [CsvColumn(FieldIndex = 4, Name = "Dept. Name")]
        public string DepartmentName { get; set; }
    
        [CsvColumn(FieldIndex = 5, Name = "Station #")]
        public string StationNumber { get; set; }
    
        [CsvColumn(FieldIndex = 6, Name = "Highest Education Level")]
        public string HighestEducationLevel { get; set; }
    
        [CsvColumn(FieldIndex = 7, Name = "Certification/Rank")]
        public string CertificationAndRank { get; set; }
    
        [CsvColumn(FieldIndex = 8, Name = "Employment Status")]
        public string EmploymentStatus { get; set; }
    
        [CsvColumn(FieldIndex = 9, Name = "Registration Date")]
        public DateTime RegistrationDate { get; set; }
    
        [CsvColumn(FieldIndex = 10, Name = "Date of Birth")]
        public DateTime DateOfBirth { get; set; }
    
        [CsvColumn(FieldIndex = 11)]
        public int Age { get; set; }
    
        [CsvColumn(FieldIndex = 12)]
        public string Citizenship { get; set; }
    
        [CsvColumn(FieldIndex = 13)]
        public string Race { get; set; }
    
        [CsvColumn(FieldIndex = 14)]
        public string Ethnicity { get; set; }
    
        [CsvColumn(FieldIndex = 15, Name = "Home Address")]
        public string HomeAddressLine1 { get; set; }
    
        [CsvColumn(FieldIndex = 16, Name = "City")]
        public string HomeAddressCity { get; set; }
    
        [CsvColumn(FieldIndex = 17, Name = "State")]
        public string HomeAddressState { get; set; }
    
        [CsvColumn(FieldIndex = 18, Name = "Zip")]
        public string HomeAddressZip { get; set; }
    
        [CsvColumn(FieldIndex = 19, Name = "County")]
        public string HomeAddressCounty { get; set; }
    
        [CsvColumn(FieldIndex = 20, Name = "Home Phone")]
        public string HomePhone { get; set; }
    
        [CsvColumn(FieldIndex = 21, Name = "Work Phone")]
        public string WorkPhone { get; set; }
    }
    
    
    // ASPX page to serve csv file
    
    protected void Page_Load(object sender, EventArgs e)
    {
        var courseId = int.Parse(Request["id"]);
        var course = Course.Queryable.Single(x => x.Id == courseId);
        Response.ContentType = "text/csv";
        Response.AddHeader("Content-Disposition", string.Format("attachment;filename=\"{0}.csv\";", course.Code));
    
        using (var writer = new System.IO.StreamWriter(Response.OutputStream))
        {
            var registrations = Mapper.Map<IEnumerable<Registration>, IEnumerable<CsvRegistration>>(course.Registrations);
            var cc = new LINQtoCSV.CsvContext();
            cc.Write(registrations, writer);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a record that I write out to a CSV file using FileHelpers.
Using C#, I need a class called User that has a username, password, active
Using ASP.NET MVC there are situations (such as form submission) that may require a
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Using online interfaces to a version control system is a nice way to have
Using PyObjC , you can use Python to write Cocoa applications for OS X.
Using C# .NET 3.5 and WCF, I'm trying to write out some of the
Using C# and System.Data.SqlClient, is there a way to retrieve a list of parameters
Using VS2008, C#, .Net 2 and Winforms how can I make a regular Button
Using JDeveloper , I started developing a set of web pages for a project

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.