Everywhere I look seems to have the same response but I can't find one to address the issue I am having. I am trying to concatenate the items in a list of objects into a string. However, what I get is the name of the page and the name of the object, the actual list values.
I tried:
string combinedLog = string.Join(",", logList)
I also tried:
string combinedLog = string.Join(",", logList.Select(c => c.ToString()).ToArray<string>());
What I get is PageName + Log, PageName + Log
This is the object:
private class Log
{
public DateTime LogTime { get; set; }
public string Result { get; set; }
public string ItemName { get; set; }
public Guid? ItemId { get; set; }
public string ErrorMessage { get; set; }
}
and the list is:
List<Log> logList = new List<Log>();
I am trying to get a string like: "10/21/2019, Fail, Acme, Could not Import, 10/21/2019, Success, ABC, no errors"
You can override ToString()
method in Log
class for that
private class Log
{
public DateTime LogTime { get; set; }
public string Result { get; set; }
public string ItemName { get; set; }
public Guid? ItemId { get; set; }
public string ErrorMessage { get; set; }
public override string ToString()
{
return $"{LogTime}, {Result}, {ItemName}, {ErrorMessage}";
}
}
And than concatenate logList
into one string using string.Join
You should do something like String.Join(";", logList.Select(x => $"{x.LogTime},{x.Result},{x.ItemName}"))..
Or use generics to get it
var fields = typeof(Log).GetFields();
var result = String.Join(";", logList.Select(x =>
String.Join(",", fields.Select(f => f.GetValue(x)))
));
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.