I am stuck with this problem from several evenings. I have an SQLite database in my application. I created this SQLite DB from a file. ERD chart is shown below: 
And now in my application, I am creating a connection to my database:
using (var conn = new SQLiteConnection(DB_PATH)) {
I created classes that represent tables in my DB:
public class Kantory { public Kantory() { this.kursy = new HashSet<Kursy>(); } [SQLite.PrimaryKey, SQLite.AutoIncrement] public int id_kantory { get; set; } public string nazwa { get; set; } public virtual ICollection<Kursy> kursy { get; set; } } public class Waluty { public Waluty() { this.kursy = new HashSet<Kursy>(); } [SQLite.PrimaryKey, SQLite.AutoIncrement] public int id_waluty { get; set; } public string nazwa { get; set; } public virtual ICollection<Kursy> kursy { get; set; } } public class Kursy { [SQLite.PrimaryKey, SQLite.AutoIncrement] public int id_kursy { get; set; } public int id_kantory { get; set; } public int id_waluty { get; set; } public decimal kurs { get; set; } public System.DateTime data { get; set; } public int aktualne { get; set; } public virtual Kantory kantory { get; set; } public virtual Waluty waluty { get; set; } }
As you can see, in the kursy table I have two foreign keys - id_kantory and id_waluty .
And now a very curious and strange thing is happening . When I try to get some information using regular SQL expressions using the INNER JOIN statement, it works fine:
using (var conn = new SQLiteConnection(DB_PATH)) { var query = new SQLiteCommand(conn); query.CommandText = "SELECT * FROM Kursy INNER JOIN Kantory ON Kursy.id_kursy=Kantory.id_kantory WHERE Kantory.id_kantory = 1"; var result = query.ExecuteQuery<Kursy>(); }
This code works great! BUT , when I try to use my classes using LINQ, for example:
using (var conn = new SQLiteConnection(DB_PATH)) { var result = conn.Table<Kursy>().Where(k => k.kantory.id_kantory == 1).FirstOrDefault(); }
This throws me a NotSupportedException! The message looks like this: When accessing a member, the expression could not be compiled
BUT, when I use my classes using LINQ WITHOUT CONNECTING another class, this works:
using (var conn = new SQLiteConnection(DB_PATH)) { var result = conn.Table<Kursy>().Where(k => k.id_kursy == 1).FirstOrDefault(); }
Bottom line: my main problem is that I cannot join more than one table using the LINQ query . This model seems to be wrong in the classes, but I really don't know why ...
PS. This application is for Windows Phone 8.1, so I can not use the Entity Framework for this.