/* private List<Rent> GetWithDecadeGenre2(int year) * { * using (var context = DbContextCreator.Create()) * { * * Dictionary<int, int> birthyears * = context.Customers.ToDictionary(x => x.CustomerId, x => x.BirthYear); * * Dictionary<int, string> genres = context.Books.ToDictionary(x => x.BookId, x => x.Genre); * * var query = from x in context.Rents * where x.RentDate.Year == year * select x; * * List<Rent> list = query.ToList(); * * foreach(var rent in list) * { * rent.Genre = genres[rent.BookId]; * * //rent.Genre = genres[1] //1, 역사 * //rent.Genre = genres[2] //2, 문학 * * rent.BirthYear = birthyears[rent.CustomerId]; * } * * return list; * } * }*/ /// <summary> /// 연령별로 성별 리스트화 메소드 /// </summary> /// <param name="borrow"></param> /// <returns></returns> public List <DecadeExtraSummary> DecadeGenderSearch(string borrow) { List <Rent> rents = GetWithDecadeGender(borrow); List <DecadeExtraSummary> models = new List <DecadeExtraSummary>(); foreach (Rent rent in rents) { DecadeExtraSummary summary = models.Find(x => x.Decade == rent.Decade && x.Gender == rent.Gender); if (summary != null) { summary.Count++; } else { summary = new DecadeExtraSummary(); summary.Count = 1; summary.Decade = rent.Decade; summary.Gender = rent.Gender; models.Add(summary); } } return(models); //models를 반환함. }
/// <summary> /// 연령별로 장르 구분하는 메소드 /// </summary> /// <param name="borrow"></param> /// <returns></returns> public List <DecadeExtraSummary> DecadeGenreSearch(int borrow) //데이터를 리스트화해서 찾는 메소드 { List <Rent> rents = GetWithDecadeGenre(borrow); //Rent데이터를 리스트화 한 매개변수를 rents. //GetWithExtra라는 메소드에서 생긴 리스트를 rents에 저장. List <DecadeExtraSummary> models = new List <DecadeExtraSummary>(); foreach (Rent rent in rents) //Rents에 있는 rent를 돌린다. { DecadeExtraSummary summary = models.Find(x => x.Decade == rent.Decade && x.Genre == rent.Genre); //매개변수 summary = models에 있는 리스트를 찾는다.(Find) //foreach에서 생성한 매개변수 x에 있는 Decade 값이 Rents에 있는 Decade 값도 동일한지 체크. // Genre 값이 Rents에 있는 Genre 값이 동일한지 체크. // ex) 10대, 문학(생성) / 10대, 역사(생성) / 20대, 문학(생성) if (summary != null) { summary.Count++; } //summary값이 null아닐 경우 count를 올려줌. //null값일 경우 else { summary = new DecadeExtraSummary(); summary.Count = 1; summary.Decade = rent.Decade; summary.Genre = rent.Genre; models.Add(summary); } //count = 1, Decade, Genre을 models에 추가(Add). } return(models); //models를 반환함. }