marcos marcos - 4 months ago 49
C# Question

SQLite.Net-PCL No such function: ToString

I have Employees table with

Name
(string) and
Number
(int) columns.

I can search for employees for anything. So,

var query = cnn.Table<Employees>()
.Where(x => x.Name.Contains(search) ||
x.Number.ToString().Contains(search));



An exception of type 'SQLite.Net.SQLiteException' occurred in SQLite.Net.Platform.WinRT.dll but was not handled in user code

Additional information: no such function: tostring


How can I cast Number column to string?

Thank you.

Answer

The problem is that SQLLite linq doesn't know what is ToString() - that is a C# method that can be overridden to do anything. You should use SqlFunctions:

var query = cnn.Table<Employees>()
               .Where(x => x.Name.Contains(search) ||              
                           SqlFunctions.StringConvert((int)x.Number).Contains(search));

Another solution can be to add a .ToList()/AsEnumerable() before the where but then that filtering will happen in the c# instead of the database


One can also:

var query = cnn.Table<Employees>()
               .Where(x => x.Name.Contains(search)
               .AsEnumerable()
               .Where( x => x.Number.ToString().Contains(search));