If I have a DataRow list and use a foreach statement to create a string containing information in a list format and I want the last item in the list to be preceded by the string " and " how could I accomplish this within the foreach statement or is that not possible?
For example, if my data row contained the numbers between 1 and 10, and my foreach statement extracted the even numbers and put them in a string, I want the output of the string to be:
2, 4, 6, 8 and 10
instead of:
2, 4, 6, 8, 10
Right now I am creating a string list of locations in my locations table, here is my foreach statement as it stands now:
foreach (DataRow xLocation in locationsList.GetLocationsList)
{
if (L_String == string.Empty)
{
L_String += xLocation["L_Name"].ToString();
}
else
{
L_String += ", " + xLocation["L_Name"].ToString();
}
}
the last location should be preceded by the word "and" instead of the "," any ideas?