public List <LastPictures> GetLastPictures(int id, int count) { if (count == 0) { count = 999; } var plants = _plantRepository.GetAllIncludingLabfarm(); var plants2 = new List <Plant>(); foreach (Plant s in plants) { if (s.Labfarm.Id == id) { plants2.Add(s); } } var lastPictures = new List <LastPictures>(); foreach (Plant s in plants2) { s.Pictures = s.Pictures.OrderByDescending(x => x.TimeStamp.TimeOfDay) .ThenBy(x => x.TimeStamp.Date) .ThenBy(x => x.TimeStamp.Year) .ToList(); var d = new LastPictures(); d.Name = s.Name; d.Pictures = s.Pictures.ToList(); //TODO mapper? if (count < d.Pictures.Count) { d.Pictures.RemoveRange(count, d.Pictures.Count - count); } lastPictures.Add(d); } return(lastPictures); }