private void UpdateBooksPerMonth() { // clear the list and the counts Dictionary <DateTime, List <BookRead> > bookMonths = new Dictionary <DateTime, List <BookRead> >(); if (BooksRead.Count < 1) { return; } DateTime startDate = BooksRead[0].Date; DateTime endDate = BooksRead.Last().Date; endDate = new DateTime(endDate.Year, endDate.Month, endDate.Day, 23, 59, 59); DateTime monthStart = new DateTime(startDate.Year, startDate.Month, 1); DateTime monthEnd = monthStart.AddMonths(1).AddSeconds(-1); // get all the months a book has been read while (monthStart <= endDate) { List <BookRead> monthList = new List <BookRead>(); foreach (BookRead book in BooksRead) { if (book.Date >= monthStart && book.Date <= monthEnd) { monthList.Add(book); } } if (monthList.Count > 0) { bookMonths.Add(monthStart, monthList); } monthStart = monthStart.AddMonths(1); monthEnd = monthStart.AddMonths(1).AddSeconds(-1); } TalliedMonths.Clear(); foreach (DateTime date in bookMonths.Keys.OrderBy(x => x)) { TalliedMonths.Add(new TalliedMonth(date, bookMonths[date])); } }
public bool AddNewBook(BookRead newBook, out string errorMsg) { errorMsg = ""; // for the moment insist only that the date is after the last of the existing items if (BooksRead.Last().Date > newBook.Date) { errorMsg = "Date must be after last date : " + BooksRead.Last().DateString; return(false); } BooksRead.Add(newBook); if (DataFromDb) { AddNewBookToDatabase(newBook); } UpdateCollections(); return(true); }