score:12

Accepted answer

Like this:

string delimitedString = 
           string.Join(",", tasks.Select(
                                  l => string.Format("{0}#{1}", l.TaskID, l.IsComplete)));

If you're using C#-6:

string delimitedString = 
            string.Join(",", tasks.Select(
                                   l => $"{l.TaskID}#{l.IsComplete}"));

score:2

I think what you need is to implement a ToString for the Task, so it displays what you want.

If you can't (it isnt your code), implement a separate function:

private string StringifyTask(Task task)
{
    return string.Format("{0}#{1}", task.TaskId, task.IsComplete);
}

And then use StringifyTask as the argument of Select. Also - note you don't actually need the ToList before the Linq statement.

score:5

You could do it the way Yuval Itzchakov proposed, but I'd rather implement a new method in Task that you can call - just to keep it easier to read.

public class Task
{
    ...
    public String ToDelimitedString()
    {
        return String.Format("{0}#{1}", TaskId, IsComplete);
    }
}

And then call it like this:

var delimString = String.Join(",", tasks.Select(t => t.ToDelimitedString()));

This way you have your format in one place and can call it from anywhere without inconsistencies.

By the way: I'd also try to find a different name for the class to avoid any possible confusion with System.Threading.Tasks.Task.


Related Query

More Query from same tag