Sep 22
2009

Here a simple example to get a commaseparated list of cities to show them in the UI.
The result could be "New York, Madrid, Rome and Munich".

/// <summary>
/// commaseparated string of location cities
/// </summary>
        public string LocationsCitiesAsString
        {
            get
            {
                string strVacLocations = null;

                //put all vacancyLocations commaseparated to a string:
                foreach (var location in Vacancy.Locations)
                {
                     strVacLocations += location.City + ", ";
                }

                //if existing delete comma and whitespace at the end of string:
                if (strVacLocations.EndsWith(", "))
                {
                    strVacLocations = strVacLocations.Remove(strVacLocations.Length - 2, 2);
                }

                //replace last ", " with " and":
                int lastSpace = strVacLocations.LastIndexOf(", ");
                if (lastSpace > 0)
                {
                    strVacLocations = strVacLocations.Remove(lastSpace, 2).Insert(lastSpace, " and ");
                }

                return strVacLocations;
            }
        }

Tags: Tags: