连接GetWaypoints
方法生成的列表的最佳方法是什么?我想连接JobWaypoints
下的所有项目,并用新的行分隔它们。
我似乎无法找到直接访问我创建的JobWaypoints
列表中waypoints
项的最佳方法。
Window.cs
List<JobComponent.JobList> waypoints = JobComponent.GetWaypoints(jobId);
JobComponents.cs
public static List<JobList> GetWaypoints(int jobId)
{
// Get destination
const string query = "SELECT address FROM waypoint " +
"WHERE id NOT IN(" +
"(SELECT MIN(ID) FROM waypoint where booking_id=@id)," +
"(SELECT MAX(ID) FROM waypoint where booking_id=@id)) " +
"AND booking_id=@id";
var jobList = new List<JobList>();
using (var cmd = new MySqlCommand(query, DbObject.Connection))
{
if (DbObject.Connection.State != ConnectionState.Open)
{
DbObject.OpenConnection();
}
cmd.Parameters.AddWithValue(("@id"), jobId);
try
{
using (MySqlDataReader dataReader = cmd.ExecuteReader())
{
while (dataReader.Read())
{
var item = new JobList
{
JobWaypoints = dataReader["address"] + ""
};
jobList.Add(item);
}
dataReader.Close();
DbObject.CloseConnection();
return jobList;
}
}
catch (Exception ex)
{
ErrorHandlingComponent.LogError(ex.ToString());
throw;
}
}
}
答案 0 :(得分:1)
我使用了string.Join方法。以下示例将创建一个字符串,其中包含所有JobWaypoints值,并在每个值之间使用逗号(,):
List<JobComponent.JobList> waypoints = JobComponent.GetWaypoints(jobId);
string s = string.Join(",", waypoints.Select(i => i.JobWaypoints));