praeclarum / praeclarum/sqlite-net

Support for .StartsWith with Sqlite Async

Open
#696 1 comment 0 reactions 1 assignee View on GitHub

@praeclarum is already working on this.

Since May 11, 2018.

Feature
Dominant language
C#
Stars
4.5k
Forks
1.4k
PR merge metrics
No merged PRs in 30d

Description

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();
        }

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.