Ejemplo n.º 1
0
        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;
            dataGridViewBooks.DataSource       = bindingSourceBookSearch;
        }
Ejemplo n.º 2
0
        private Task <List <Book> > SearchBooks(BookSearchForm bookSearchForm)
        {
            return(Task.Run(() => {
                List <Book> searchResult = new List <Book>();
                IQueryable <Book> bookIQuer = _db.Books;
                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;
            }));
        }