Reputation: 1040
Is there a quicker or more efficient way to add Strings to a List than the below example?:
List<String> apptList = new List<String>();
foreach (Appointment appointment in appointments){
String subject = appointment.Subject;
//...(continues for another 10 lines)
//...And then manually adding each String to the List:
apptList.Add(subject);
//...(continues for another 10 lines)
//And then send off List apptList to another method
}
Upvotes: 3
Views: 149
Reputation: 15148
How about this:
List<string> apptList = appointments.Select(x => x.Subject).ToList();
Upvotes: 1
Reputation: 13
I'm not sure if I'm getting your code right, but since your Appointment class is already implementing IEnumerable, you should be able to call ToList() to convert it to a list in one shot.
http://msdn.microsoft.com/en-us/library/bb342261.aspx
Upvotes: 1
Reputation: 44288
var apptList = appointments.Select(a => a.Subject).ToList();
Upvotes: 6