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 7436239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:12:33+00:00 2026-05-29T10:12:33+00:00

Answer Summary: Solved this problem using Jon Skeet’s answer below. Here is the finished

  • 0

Answer Summary:

Solved this problem using Jon Skeet’s answer below. Here is the finished code

    public static CSVData CreateCSVData(List<RegDataDisplay> rList, 
                                                         string[] selectors)
    {
        CSVData csv = new CSVData(); // Create the CSVData object
        foreach(string selector in selectors)
        {
            // Get the PropertyInfo for the property whose name
            // is the value of selector
            var property = typeof(RegDataDisplay).GetProperty(selector); 

            // Use LINQ to get a list of the values for the specified property
            // of each RegDataDisplay object in the supplied list.
            var values = rList.Select(row => property.GetValue(row, null)
                              .ToString());

            // Create a new list with the property name to use as a header
            List<string> templs = new List<string>(){selector};

            // Add the returned values after the header 
            templs.AddRange(values);

            // Add this list as a column for the CSVData object.
            csv.Columns.Add(templs);
        }
        return csv;
    }

Question

I am building my SQL query dynamically from user input, and then exporting the results to a CSV file. I have a class called RegDataDisplay which has a property for each of the possible columns returned by my query. I can tell what columns are being selected but in my CSV creator I need to be able to only output those specific columns.

In the example below, all of the data I have retrieved is in rList, and the names of the properties I need are in selectors. So I want to iterate through the list and then add only the properties I need to my CSV data.

public static CSVData CreateCSVData(List<RegDataDisplay> rList, string[] selectors)
{ 
    CSVData csv = new CSVData();
    for(int i = 0; i < selectors.Length; i++)
    {
        csv.Columns.Add(new List<string>(){selectors[i]});
    }

    // So now I have the headers for the CSV columns, 
    // I need the specific properties only which is where I'm stuck

    for(int i = 0; i < selectors.Length; i++)
    {
        for(int j = 0; j < rList.Count; j++)
        {
            // If it was javascript I would do something like this 

            csv.Columns[i].Add(rList[j][selectors[i]]);

        } 
    }
}

Thanks

EDIT: On the right track now but I’m coming up against an error “Object does not match target type”.

    public static CSVData CreateCSVData()
    {
        // I've created a test method with test data

        string[] selectors = new string[] { "Firstname", "Lastname" };
        List<RegDataDisplay> rList = new List<RegDataDisplay>();
        RegDataDisplay rd = new RegDataDisplay();
        rd.Firstname = "first";
        rd.Lastname = "last";
        rList.Add(rd);

        CSVData csv = new CSVData();
        foreach(string selector in selectors)
        {
            var property = typeof(RegDataDisplay).GetProperty(selector);
            var values = rList.Select(row => property.GetValue(rList, null).ToString())
                              .ToList(); // Error throws here
            csv.Columns.Add(values);
        }
        return csv;
    }
  • 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-29T10:12:34+00:00Added an answer on May 29, 2026 at 10:12 am

    Assuming you’re on .NET 3.5 or higher, it sounds like you may want something like:

    public static CSVData CreateCSVData(List<RegDataDisplay> rList,
                                        string[] selectors)
    { 
        CSVData csv = new CSVData();
        foreach (string selector in selectors)
        {
            var prop = typeof(RegDataDisplay).GetProperty(selector);
            var values = rList.Select(row => (string) prop.GetValue(row, null))
                              .ToList();
            csv.Columns.Add(values);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(See below solution I created using the answer I accepted) I'm trying to improve
Post-Answer-Acceptance Summary: The problem was the use of a pointer to a stack variable
I searched here for the answer. I'm sorry if this has been asked before
I have this class you see below in the answer. There I can refer
Since this is a huge post here is a short summary (please read the
I had expected to find an answer easily to this problem, but I didn't.
ANSWER: If you ever see these lines and are mistified like I was, here's
Answer : Implemented using Curl... $file = http://abc.com/data//output.txt; $ch = curl_init($file); $fp = @fopen(out.txt,
This answer says that Linq is targeted at a slightly different group of developers
In answer to this question Joel Coehoorn said Finally, only after the site's gone

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.