I'm receiving an error in this line:
``` C#
// this works
return db.Table
// this doesn't
return db.Table
This part of my code:
``` C#
public class Preferences
{
class Preference
{
[PrimaryKey]
public string Key { get; set; }
public string Value { get; set; }
public Preference(){}
public Preference(string key, string value) {
Key = key;
Value = value;
}
}
static bool init;
static SQLiteConnection DBConnection {
get { return InitDB(CreateConnection("Preferences")); }
}
static SQLiteConnection CreateConnection(string dbname) {
var nameFileDB = dbname + "_db";
var docsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var pathToDB = Path.Combine(docsFolder, nameFileDB);
return new SQLiteConnection(pathToDB);
}
static SQLiteConnection InitDB(SQLiteConnection conn) {
if (!init) {
conn.CreateTable<Preference>();
init = true;
}
return conn;
}
public static List<string> AllKeys() {
using (var db = DBConnection) {
// this works
// return db.Table<Preference>().ToList().Select(p => p.Key).ToList();
// this doesn't
return db.Table<Preference>().Select(p => p.Key).ToList();
}
}
}
This is part of my class, and I'm getting the error after I insert some values on the table and then call Preferences.AllKeys() function
StackTrace:
[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] System.MissingMethodException: Default constructor not found for type System.String
[MonoDroid] at System.RuntimeType.CreateInstanceMono (Boolean nonPublic) [0x00085] in /Users/builder/data/lanes/2512/d3008455/source/mono/mcs/class/corlib/ReferenceSources/RuntimeType.cs:105
[MonoDroid] at System.RuntimeType.CreateInstanceSlow (Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, System.Threading.StackCrawlMark& stackMark) [0x0001a] in /Users/builder/data/lanes/2512/d3008455/source/mono/mcs/class/corlib/ReferenceSources/RuntimeType.cs:87
[MonoDroid] at System.RuntimeType.CreateInstanceDefaultCtor (Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, System.Threading.StackCrawlMark& stackMark) [0x0002a] in /Users/builder/data/lanes/2512/d3008455/source/mono/external/referencesource/mscorlib/system/rttype.cs:5734
[MonoDroid] at System.Activator.CreateInstance (System.Type type, Boolean nonPublic) [0x00040] in /Users/builder/data/lanes/2512/d3008455/source/mono/external/referencesource/mscorlib/system/activator.cs:214
[MonoDroid] at System.Activator.CreateInstance (System.Type type) [0x00000] in /Users/builder/data/lanes/2512/d3008455/source/mono/external/referencesource/mscorlib/system/activator.cs:147
[MonoDroid] at SQLite.SQLiteCommand+<ExecuteDeferredQuery>d__0`1[T].MoveNext () [0x000d3] in g:\Project1\Models\Database\SQLite.cs:2203
[MonoDroid] at System.Collections.Generic.List`1[T]..ctor (IEnumerable`1 collection) [0x0008b] in /Users/builder/data/lanes/2512/d3008455/source/mono/external/referencesource/mscorlib/system/collections/generic/list.cs:105
[MonoDroid] at System.Linq.Enumerable.ToList[TSource] (IEnumerable`1 source) [0x00011] in /Users/builder/data/lanes/2512/d3008455/source/mono/external/referencesource/System.Core/System/Linq/Enumerable.cs:835
[MonoDroid] at SQLite.SQLiteCommand.ExecuteQuery[T] () [0x00001] in g:\Project1\Models\Database\SQLite.cs:2161
[MonoDroid] at SQLite.TableQuery`1[T].GetEnumerator () [0x0000b] in g:\Project1\Models\Database\SQLite.cs:2992
[MonoDroid] at System.Collections.Generic.List`1[T]..ctor (IEnumerable`1 collection) [0x00073] in /Users/builder/data/lanes/2512/d3008455/source/mono/external/referencesource/mscorlib/system/collections/generic/list.cs:104
[MonoDroid] at System.Linq.Enumerable.ToList[TSource] (IEnumerable`1 source) [0x00011] in /Users/builder/data/lanes/2512/d3008455/source/mono/external/referencesource/System.Core/System/Linq/Enumerable.cs:835
[MonoDroid] at SQLite.Preferences.Preferences.AllKeys () [0x00008] in g:\Project1\Preferences.cs:27
[MonoDroid] at Android.Support.V4.App.FragmentActivity.n_OnCreate_Landroid_os_Bundle_ (IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState) [0x00011] in <filename unknown>:0
[MonoDroid] at (wrapper dynamic-method) System.Object:d8fe4472-f53e-45af-8812-e33bedf2f3cf (intptr,intptr,intptr)
[art] JNI RegisterNativeMethods: attempt to register 0 native methods for md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable
Target OS: Android
Xamarin Studio: Version 5.10.1 (build 6)
Runtime:
Microsoft .NET 4.0.30319.42000
GTK+ 2.24.23 (MS-Windows theme)
GTK# 2.12.30
Xamarin.Android: Version: 6.0.0 (Business Edition)
SDK Tools Version: 24.4.1
SDK Platform Tools Version: 23.1
SDK Build Tools Version: 23.0.1
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)
Dev OS: Windows 8.1 (64-bit)
Same issue with anonymous object. This doesn't work:
db.Table<Video>().Where(...).Select(v => new { Summary = v.Summary, Id = v.Id }).ToList();
This works:
db.Table<Video>().Where(...).ToList().Select(v => new { Summary = v.Summary, Id = v.Id }).ToList();
Any news? It raised an Stack Overflow question:
https://stackoverflow.com/questions/44350374/is-performing-tolist-more-than-once-is-a-bad-idea
This will be fixed in the 1.5 series.
Fixed in: https://github.com/praeclarum/sqlite-net/commit/2c5f1e1337fddd9f1ae62e3af9828693f2c21d89
Most helpful comment
This will be fixed in the 1.5 series.
Fixed in: https://github.com/praeclarum/sqlite-net/commit/2c5f1e1337fddd9f1ae62e3af9828693f2c21d89