I have implemented twitter's typeahead plugin for form auto completion on my application.
When querying the database for a match to the name entered in the input box, it would only match case sensitive names, however typeahead is supposed to match case insensitive.
example: "Kevin S" typeahead suggests Kevin Smith, but "kevin s" typeahead suggests nothing.
API code
// GET: /api/authors
public IHttpActionResult GetAuthors(string query = null)
{
var authorsQuery = _context.Authors.ToList().Select(Mapper.Map<Author, AuthorDto>);
if (!String.IsNullOrWhiteSpace(query))
authorsQuery = authorsQuery.Where(c => c.Name.Contains(query));
return Ok(authorsQuery);
}
I thought it had something to do with the .ToList() executing before querying the database with my .Where() call.
I changed my code to this and it now works
// GET: /api/authors
public IHttpActionResult GetAuthors(string query = null)
{
var authorsQuery = _context.Authors.AsQueryable();
if (!String.IsNullOrWhiteSpace(query))
authorsQuery = authorsQuery.Where(c => c.Name.Contains(query));
var authorsDto = authorsQuery.ToList().Select(Mapper.Map<Author, AuthorDto>);
return Ok(authorsDto);
}
so when entering "kevin s" typeahead suggests "Kevin Smith"
I tried with AsEnumerable instead of AsQueryable() and it had the same effect as the original code
why is it that it works with AsQuerable but not the others? is it to do with query execution?