gpt4 book ai didi

c# - 逗号分隔字符串的对象

转载 作者:太空宇宙 更新时间:2023-11-03 17:19:43 25 4
gpt4 key购买 nike

有没有办法让逗号与对象分开。注意它的对象不是对象列表

例如:

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; }
}

具有以下值

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

这应该导致

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

做这个的最好方法是什么。可能是单行代码,因为我不想使用字符串生成器并附加所有代码。

最佳答案

我认为您正在寻找重写的 .ToString() 方法。你必须像这样修改类:

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

关于c# - 逗号分隔字符串的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49124618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com