简体   繁体   中英

Object to comma separated string

is there a way to make a comma separated from an object. Note its object not List of object

Ex:

public class EmployeeLogReportListViewModel
{
    public DateTime Date { get; set; }
    public int EmployeeID { get; set; }
    public TimeSpan Time { get; set; }
    public int Sort { get; set; }
    public string Employer { get; set; }
}

With the following values

Date = "2018/02/03"
EmployeeID = 111
Time = 11:53 AM
Sort = 1
Employer = EMP

this should result into

2018/02/03,111,11:53 AM,1 EMP

What is the best way to make this. possible single line of code cause i dont want to use string builder and append all of it.

I think you are looking for Overridden .ToString() method. you have to modify the class like this:

public class EmployeeLogReportListViewModel
{
    public DateTime Date { get; set; }
    public int EmployeeID { get; set; }
    public TimeSpan Time { get; set; }
    public int Sort { get; set; }
    public string Employer { get; set; }
    public override string ToString()
    {
        return String.Format("{0},{1},{2},{3},{4}", this.Date, this.EmployeeID, this.Time, this.Sort, this.Employer);
    }
}

Usage Example :

EmployeeLogReportListViewModel objVm = new EmployeeLogReportListViewModel();
// Assign values for the properties
objVm.ToString(); // This will give you the expected output

Challenge accepted

var o = new EmployeeLogReportListViewModel();
var text = string.Join
(
    ",",
    typeof(EmployeeLogReportListViewModel)
        .GetProperties(BindingFlags.Instance | BindingFlags.Public)
        .Select
        (
            prop => prop.GetValue(o).ToString()
        )
);
Console.WriteLine(text);

Technically that is one line.

If you want the properties sorted alphabetically, you could use this:

var o = new EmployeeLogReportListViewModel();
var text = string.Join
(
    ",",
    typeof(EmployeeLogReportListViewModel)
        .GetProperties(BindingFlags.Instance | BindingFlags.Public)
        .OrderBy( prop => prop.Name )
        .Select
        (
            prop => prop.GetValue(o).ToString()
        )
);
Console.WriteLine(text);

It's a bit late to reply, but I can imagine you want to do that in order to have some kind of csv output style

a nice and generic way to do it is to create a extension method that transform any Enumerable into a csv string.

so borrowing @John Wu's solution, we come up with something like

public static class EnumerableToCsvExtension
{
    public static string ToCSVString<TContent>(this IEnumerable<TContent> enumerable, char propertySeparator = ',', bool includeHeader = true)
    {
        var properties = typeof(TContent).GetProperties(BindingFlags.Instance | BindingFlags.Public);

        var header = string.Join(propertySeparator, properties.Select(p => p.Name));
        var rows = enumerable.ToList().ConvertAll(item => string.Join(propertySeparator, properties.Select(p => p.GetValue(item) ?? string.Empty )));

        var csvArray = includeHeader ? rows.Prepend(header) : rows;

        return string.Join(Environment.NewLine, csvArray);
    }
}

then you use it like

var list = new List<EmployeeLogReportListViewModel> { new EmployeeLogReportListViewModel(), new EmployeeLogReportListViewModel() };
list.ToCSVString();
list.ToCSVString(propertySeparator: '|', includeHeader: false);

You can use like below:

public class bKashAccountInfo
{
    public string bKashNumber { get; set; }
    public string bKashAccountType { get; set; }

    public override string ToString()
    {
        return String.Format($"bKash Number-{bKashNumber}, Account Type - {bKashAccountType}");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM