-1

What is the best way of concatenating the list produced by the GetWaypoints method? I'd like to concatenate all of the items under JobWaypoints and seperate them by a new line.

I cannot seem to figure out the best way of directly accessing the JobWaypoints item in the waypoints list I created.

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;
            }
        }
    }
methuselah
  • 12,766
  • 47
  • 165
  • 315
  • 2
    Please explain what _exactly_ you want to do ("concatenating the list" is too vague) and what you have tried to do so. Is the method querying the database even relevant? Something with `string.Join()` and `waypoints.Select()` should do the trick. – CodeCaster Feb 12 '15 at 15:14
  • Sorry. Basically what I do is run an SQL query and the results of the query are stored in a list. Now I want to convert that list into a string, inwhich item is seperated by a new line. – methuselah Feb 12 '15 at 15:18
  • 1
    Please show the code that doesn't seem to be working. This being your 500+th question you should know that. See also [Using Linq to concatenate strings](http://stackoverflow.com/questions/217805/using-linq-to-concatenate-strings), [LINQ select one field from list of DTO objects to array](http://stackoverflow.com/questions/22539581/linq-select-one-field-from-list-of-dto-objects-to-array). – CodeCaster Feb 12 '15 at 15:21
  • I can't seem to figure out how to access the `JobWaypoints` in the `waypoints` list. That's all I am really struggling with. In the past, I would cast `(JobComponent.GetWaypoints)` but this only seems to work in a situation when you've actually selected an item from the list. – methuselah Feb 12 '15 at 15:27
  • There is no `JobWaypoints` in `waypoints`. You return a list of `JobList`, only having their `JobDestination` set. – CodeCaster Feb 12 '15 at 15:32
  • Sorry - my mistake. I've updated the question. – methuselah Feb 12 '15 at 15:33

1 Answers1

1

I used the string.Join method. The following example will create a string with all JobWaypoints values with a comma (,) in between each value:

List<JobComponent.JobList> waypoints = JobComponent.GetWaypoints(jobId);
string s = string.Join(",", waypoints.Select(i => i.JobWaypoints));
methuselah
  • 12,766
  • 47
  • 165
  • 315