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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:17:45+00:00 2026-06-09T16:17:45+00:00

I’ve been writing this out a few different places in my code and I

  • 0

I’ve been writing this out a few different places in my code and I want to create a single, generic function, but I can’t quite figure out how.

Here is an example of what I’m doing:

public static string FormatCompaniesAsCSV(ICollection<Company> companies)
{
    string csv = "";
    foreach(Company c in companies)
    {
        csv += c.CompanyName + ",";
    }

    return csv.Substring(0, csv.LastIndexOf(","));
}

The problem is that I have to write essentially the same code if I want to display all company SKUs or all of the factories where this is manufactured. The only thing that changes is the object type and the property I am accessing. What I would like to do is something like the following:

public static string FormatCSV<T>(ICollection<T> elements, string propertyName)
{
    string csv = "";
    foreach(T element in elements)
    {
        //I know this next line doesn't work
        //this is just a high level conceptualization of what I want to do
        csv += element.getPropertyValue(propertyName) + ","; 
    }

    return csv.Substring(0, csv.LastIndexOf(","));
}

I need some way to access the properties of the element in the foreach loop, but I don’t really know where to start. I’ve briefly looked at the System.Linq.Expressions library, but I didn’t find quite what I was looking for there.

Am I on the right track? If not, is there a better way to accomplish what I would like to do?

  • 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-06-09T16:17:46+00:00Added an answer on June 9, 2026 at 4:17 pm

    I suggest you pass in a delegate:

    public static string FormatAsCSV<T>(IEnumerable<T> elements,
                                        Func<T, string> converter) 
    { 
        string csv = ""; 
        foreach(T element in elements) 
        { 
            csv += converter(element) + ","; 
        } 
    
        return csv.Substring(0, csv.LastIndexOf(",")); 
    } 
    

    You can call this method like this:

    FormatAsCsv(companies, x => x.CompanyName);
    FormatAsCsv(factories, x => x.ZipCode + " " + x.City);
    

    You could even create an extension method out of it by adding this before the type of the first parameter:

    public static string FormatAsCSV<T>(this IEnumerable<T> elements,
                                        Func<T, string> converter) 
    

    Now you can call it like this:

    companies.FormatAsCsv(x => x.CompanyName);
    factories.FormatAsCsv(x => x.ZipCode + " " + x.City);
    

    BTW: I changed the type from ICollection<T> to IEnumerable<T> as you are only using features of IEnumerable<T>.

    And you can improve the generation of the CSV string:

    public static string FormatAsCSV<T>(this IEnumerable<T> elements,
                                            Func<T, string> converter) 
    {
        return elements.Select(x => converter(x)).Aggregate((x, y) => x + "," + y);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to construct a data frame in an Rcpp function, but when I
I want to count how many characters a certain string has in PHP, but
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
I'm trying to create an if statement in PHP that prevents a single post
I have a jquery bug and I've been looking for hours now, I can't

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.