public PagedResult <BookDto> GetAll(BooksSearchModel searchModel) { var query = _dataContext.Books .Include(p => p.BookAuthors) .ThenInclude(p => p.Author) .AsQueryable(); var binder = new SearchBinder <BooksSearchModel, Book>() .AddSearchMapping(p => p.Description, (book, sm) => book.Description.Contains(sm.Description, StringComparison.InvariantCultureIgnoreCase)) .AddSearchMapping(p => p.Title, (book, sm) => book.Title.Contains(sm.Title, StringComparison.InvariantCultureIgnoreCase)); query = binder .ApplySearch(query, searchModel) .OrderBy(searchModel); var totalItems = query.Count(); query = query.SkipAndTake(searchModel); var result = new PagedResult <BookDto> { ItemCount = totalItems, Items = query.Select(p => new BookDto { Id = p.Id, Title = p.Title, Description = p.Description, PublishDate = p.PublishDate, Authors = p.BookAuthors.Select(o => o.Author.Name).ToArray() }).ToArray() }; return(result); }
/// <summary> /// Parses the <paramref name="search"/> clause, binding /// the text into a metadata-bound list of properties to be selected using the provided model. /// </summary> /// <param name="search">String representation of the search expression from the URI.</param> /// <param name="configuration">The configuration used for binding.</param> /// <returns>A <see cref="SearchClause"/> representing the metadata bound search expression.</returns> private static SearchClause ParseSearchImplementation(string search, ODataUriParserConfiguration configuration) { ExceptionUtils.CheckArgumentNotNull(configuration, "configuration"); ExceptionUtils.CheckArgumentNotNull(search, "search"); SearchParser searchParser = new SearchParser(configuration.Settings.SearchLimit); QueryToken queryToken = searchParser.ParseSearch(search); // Bind it to metadata BindingState state = new BindingState(configuration); MetadataBinder binder = new MetadataBinder(state); SearchBinder searchBinder = new SearchBinder(binder.Bind); return(searchBinder.BindSearch(queryToken)); }
public static SearchBinder BuildSearchBinder(string search) { search = search.Replace(" ", "").ToLower(); var result = new SearchBinder { Name = search }; if (search.IndexOf("-", StringComparison.Ordinal) > 0) { var searchArray = search.Split('-'); result.Name = searchArray[0]; result.Id = searchArray[1]; } return(result); }