コード例 #1
0
        public IEnumerable<Book> GetFilteredSortedPage(BookFieldType sortBy, BookFieldType filterBy, string filterString)
        {
            IEnumerable<Book> initResult;
            XmlSerializer serializer = new XmlSerializer(typeof(BookList));
            lock (locker)
            {
                using (
                    StreamReader reader =
                        new StreamReader(Path.Combine(HttpContext.Current.Server.MapPath(@"~/App_Data"),
                            _booksSampleXml)))
                {
                    initResult = ((BookList)serializer.Deserialize(reader)).Books;
                }
            }
            IEnumerable<Book> filteredresult = initResult;
            IEnumerable<Book> sortedResult = initResult;

            if (!string.IsNullOrEmpty(filterString))
            {
                switch (filterBy)
                {
                    case BookFieldType.Author:
                        filteredresult = initResult.Where(x => x.Author.Contains(filterString));
                        break;

                    case BookFieldType.Title:
                        filteredresult = initResult.Where(x => x.Title.Contains(filterString));
                        break;

                    case BookFieldType.Year:
                        int year = 0;
                        if (int.TryParse(filterString, out year))
                            filteredresult = initResult.Where(x => x.Year == year);
                        break;

                }
            }

            switch (sortBy)
            {
                case BookFieldType.Author:
                    sortedResult = filteredresult.OrderBy(x => x.Author);
                    break;

                case BookFieldType.Title:
                    sortedResult = filteredresult.OrderBy(x => x.Title);
                    break;

                case BookFieldType.Year:
                    sortedResult = filteredresult.OrderBy(x => x.Year);
                    break;
            }

            return sortedResult;
        }
コード例 #2
0
        public IEnumerable<Book> GetFilteredSortedPage(BookFieldType sortBy, BookFieldType filterBy, string filterString)
        {
            IEnumerable<Book> filteredresult = bookContext.Books;
            IEnumerable<Book> sortedResult = filteredresult;

            if (!string.IsNullOrEmpty(filterString))
            {
                switch (filterBy)
                {
                    case BookFieldType.Author:
                        filteredresult = bookContext.Books.Where(x => x.Author.Contains(filterString));
                        break;

                    case BookFieldType.Title:
                        filteredresult = bookContext.Books.Where(x => x.Title.Contains(filterString));
                        break;

                    case BookFieldType.Year:
                        int year = 0; if (int.TryParse(filterString, out year))
                            filteredresult = bookContext.Books.Where(x => x.Year == year);
                        break;

                }
            }

            switch (sortBy)
            {
                case BookFieldType.Author:
                    sortedResult = filteredresult.OrderBy(x => x.Author);
                    break;

                case BookFieldType.Title:
                    sortedResult = filteredresult.OrderBy(x => x.Title);
                    break;

                case BookFieldType.Year:
                    sortedResult = filteredresult.OrderBy(x => x.Year);
                    break;
            }

            return sortedResult;
        }