private async void btnBookSearch_Click(object sender, EventArgs e) { BookSearchForm bookSearchForm = new BookSearchForm(); DialogResult result = bookSearchForm.ShowDialog(this); if (result == DialogResult.Cancel) { return; } _searchedBooks.Clear(); _searchedBooks = await SearchBooks(bookSearchForm); bindingSourceBookSearch.DataSource = _searchedBooks; dataGridViewAllBooks.DataSource = bindingSourceBookSearch; }
private Task <List <Book> > SearchBooks(BookSearchForm bookSearchForm) { return(Task.Run(() => { List <Book> searchResult = new List <Book>(); IQueryable <Book> bookIQuer = _db.Books.Where(p => p.Client == null); if (!string.IsNullOrWhiteSpace(bookSearchForm.textBoxSearchTitle.Text)) { bookIQuer = bookIQuer.Where(p => p.Title.Contains(bookSearchForm.textBoxSearchTitle.Text)); } if (!string.IsNullOrWhiteSpace(bookSearchForm.textBoxSearchAuthor.Text)) { bookIQuer = bookIQuer.Where(p => p.Author.FirstName.Contains(bookSearchForm.textBoxSearchAuthor.Text) || p.Author.SecondName.Contains(bookSearchForm.textBoxSearchAuthor.Text)); } int year = 0; if (!string.IsNullOrWhiteSpace(bookSearchForm.textBoxSearchPublishing.Text) && int.TryParse(bookSearchForm.textBoxSearchPublishing.Text, out year)) { bookIQuer = bookIQuer.Where(p => p.PublishingDate == year); } if (bookSearchForm.checkedListBoxSearchGenre.CheckedItems.Count > 0) { foreach (var item in bookSearchForm.checkedListBoxSearchGenre.CheckedItems) { BookGenre genre = (BookGenre)Enum.Parse(typeof(BookGenre), item.ToString()); searchResult.AddRange(bookIQuer.Where(p => p.Genre == genre).ToList <Book>()); } } else { searchResult.AddRange(bookIQuer.ToList <Book>()); } return searchResult; })); }