Skip to content

Support for .StartsWith with Sqlite Async  #696

@owlstack

Description

@owlstack

I'm trying to do a search where the user enters a string then find a matching string in my db.

Currently, using .Contains() for the search works just fine. E.g.

User Input: Park
Results: Central Park, Washing Square Park, Riverside Park, Park Avenue, Park Chester

However, if I use .StartsWith() instead of .Contains(), it results in a null reference exception thrown.

var dbCode = await repo.GetAsync<List<DBCode>>(_ => _.Code.Equals(id) && _.Name.ToUpper().StartsWith(searchWord.ToUpper())); //Doesn't work, throws NRE 
 var dbCode = await repo.GetAsync<List<DBCode>>(_ => _.Code.Equals(id) && _.Name.ToUpper().Contains(searchWord.ToUpper())); //works fine 

I want to use .StartsWith(), so I have the expected result below:

User Input: Park
Result: Park Avenue, Park Chester

Instead of all the words that end in park.

How can I do that with the approach I'm using above?

Exception details below:

System.NullReferenceException: Object reference not set to an instance of an object.
at SQLite.TableQuery`1.CompileExpr(Expression expr, List`1 queryArgs)
  at SQLite.TableQuery`1.CompileExpr(Expression expr, List`1 queryArgs)
  at SQLite.TableQuery`1.GenerateCommand(String selectionList)
  at SQLite.TableQuery`1.GetEnumerator()
  at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
  at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
  at SQLite.AsyncTableQuery`1.<ToListAsync>b__9_0()
  at System.Threading.Tasks.Task`1.InnerInvoke()
  at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
  at Data.SQLiteRepository`1.<GetAsync>d__3`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

As a side note, this issue also happens for .Trim() and some other linq commands as well.

Also, I am using the Generic Repository here in the first comment as well: https://stackoverflow.com/questions/29050400/generic-repository-for-sqlite-net-in-xamarin-project

Particularly:

 public async Task<List<T>> GetAsync<TValue>(Expression<Func<T, bool>> predicate = null,
            Expression<Func<T, TValue>> orderBy = null)
        {
            var query = Context.Table<T>();

            if (predicate != null)
                query = query.Where(predicate);

            if (orderBy != null)
                query = query.OrderBy<TValue>(orderBy);

            return await query.ToListAsync();
        }

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions