How to create a CSV string from a common <T> list

I have the following code to return a list of generalizations that I want to iterate over, and as a result create a list of values ​​separated by commas as a string.

public static List<ReportCriteriaItem> GetCurrentSelection(ReportWizardCriteriaType criteriaType)
    {
        return Criteria.CriteriaList.FindAll(delegate(ReportCriteriaItem item) 
                                                { return item.CriteriaType == criteriaType; }
                                            );
    }

here is the definition of ReportCriteriaItem - the object from which I am making a general list of ... I think this is the key here to get its "id" field in CSV:

public class ReportCriteriaItem
{
    [System.Xml.Serialization.XmlAttribute("id")]
    public string Id { get; set; }

    [System.Xml.Serialization.XmlAttribute("name")]
    public string Name { get; set; }

    [System.Xml.Serialization.XmlAttribute("value")]
    public string Value { get; set; }

    [System.Xml.Serialization.XmlAttribute("type")]
    public ReportWizardCriteriaType CriteriaType { get; set; }

    public ReportCriteriaItem() { }
    public ReportCriteriaItem(ReportWizardCriteriaType criteriaType, string id, string name, string value)
    {
        this.Id = id;
        this.Name = name;
        this.Value = value;
        this.CriteriaType = criteriaType;
    }
}

Can I use for each cycle for this?

+3
source share
6 answers
string list = "";
foreach (ReportCriteriaItem item in GetCurrentSelection(...)) {
    if (!string.IsNullOrEmpty(list)) list += ",";
    list += item.???;
}
// do something with the list.
+1
source

The easiest way is to use string.Join:

string joined = string.Join(",", GetCurrentSelection(...).Select(x => x.Value));

, , , # .NET , . , .

( , .NET 3.5 string.Join , .NET 4 . .NET , , ASP.NET.)

+4

, String.Join:

var myList = GetCurrentSelection(ReportWizardCriteriaType.SomeCriteria);
var csv = String.Join(",",myList.ToArray());

Voila!

+3

.net 4, String.Join <T> (, IEnumerable <T> ):

string csv = string.Join(", ", GetCurrentSelection(...));

ReportCriteriaItem, ToString(). ReportCriteriaItem ( .net 4), Jon Skeet .

+2
source

I like the combination of single-line solutions, but because of the nature of the beast, and the beast is my object, I need to loop and pull out the meaning of each common element ...

string csv = "";
            foreach (ReportCriteriaItem item in GetCurrentSelection(criteriaType))
            {
                csv += item.Id + ",";
            }
            return csv.TrimEnd(",".ToArray() ) ;
+1
source

Enjoy it!

public static class DataHelper
{
   public static string ToCsv<T>(string separator, IEnumerable<T> objectlist)
   {
                Type t = typeof(T);
                PropertyInfo[] fields = t.GetProperties();

                string header = String.Join(separator, fields.Select(f => f.Name).ToArray());

                StringBuilder csvdata = new StringBuilder();
                csvdata.AppendLine(header);

                foreach (var o in objectlist)
                    csvdata.AppendLine(ToCsvFields(separator, fields, o));

                return csvdata.ToString();
            }

public static string ToCsvFields(string separator, PropertyInfo[] fields, object o)
            {
                StringBuilder linie = new StringBuilder();

                foreach (var f in fields)
                {
                    if (linie.Length > 0)
                        linie.Append(separator);

                    var x = f.GetValue(o);

                    if (x != null)
                        linie.Append(x.ToString());
                }

                return linie.ToString();
            }
    }

Use it like this:

var customers = new List<Customer>(); // Any simple class
string csv = DataHelper.ToCsv(";",customers ); 
0
source

Source: https://habr.com/ru/post/1775240/


All Articles