private void generateStatisticData() { IEnumerable <VashmagazinApartment> apartments = _vashmagazinRepository.GetListOfLastApartments(); IEnumerable <string> districts = _vashmagazinRepository.GetDistinctDistricts(apartments); IEnumerable <Statistic> statistics = _statisticRepository.GetAllStatistics(); foreach (Statistic statistic in statistics) { statistic.IsOld = true; } _statisticRepository.SaveChanges(); foreach (string district in districts) { IEnumerable <VashmagazinApartment> apartmentsInDistrict = apartments.Where(a => (a.Address.District == district) && (a.Price > 0) && (a.TotalSquare.Value > 0) && (a.TotalSquare.Unit == SquareUnit.SquareMeters)); double p = apartmentsInDistrict.Average(a => (double)a.Price / a.TotalSquare.Value); decimal avaragePricePerMeter = (decimal)p; Statistic stat = new Statistic(); stat.District = district; stat.Date = DateTime.Today; stat.IsOld = false; stat.PricePerMeter = avaragePricePerMeter; _statisticRepository.Add(stat); } _statisticRepository.SaveChanges(); }