Ejemplo n.º 1
0
        public void PerformFiltering()
        {
            if (_filter == null)
            {
                _filter = "";
            }

            _allCities = DataRepo.AllCities;

            //Lower-case and trim string
            var lowerCaseFilter = Filter.ToLowerInvariant().Trim();

            //Use LINQ query to get all personmodel names that match filter text, as a list
            var result      = _allCities.Where(tf => tf.Name.ToLowerInvariant().Contains(lowerCaseFilter)).ToList();
            var resultCount = result.Count;

            var toRemove = new List <CityModel>();

            if (Cities.Count > 0)
            {
                toRemove = Cities.Except(result).ToList();
            }

            //Loop to remove items that fail filter
            foreach (var x in toRemove)
            {
                Cities.Remove(x);
            }

            // Add back in correct order.
            for (int i = 0; i < resultCount; i++)
            {
                var resultItem = result[i];
                if (i + 1 > Cities.Count || !Cities[i].Equals(resultItem))
                {
                    Cities.Insert(i, resultItem);
                }
            }
        }