I am using SQLite.NET Async (http://www.nuget.org/packages/SQLite.Net.Async-PCL/) in a Xamarin iOS project, but I am having a problem using the table predicate queries.
Anytime I use the Get method below, which is very simple, I receive an exception that the expression could not be compiled, System.NotSupportedException: Cannot compile: Parameter.
However, if I use a low level query, as with the GetQuery method, it works fine. Is there something I am doing wrong in the definition of my table or in the method that is preventing sqlite.net from compiling the expression?
public interface IDataModel
{
[PrimaryKey, AutoIncrement]
int Id { get; set; }
}
public class BaseDataModel : IDataModel
{
[PrimaryKey]
public virtual int Id { get; set; }
}
[Table("Event")]
public class EventDataModel : BaseDataModel
{
public string Name { get; set; }
public int OrganizationId { get; set; }
public DateTime StartDate { get; set; }
public DateTime? EndDate { get; set; }
public bool Active { get; set; }
}
public class DataService<T> : IDataService<T> where T : IDataModel, new()
{
public virtual async Task<T> Get(int id)
{
var connection = await GetConnection();
return await connection.Table<T>()
.Where(item => item.Id == id)
.FirstOrDefaultAsync();
}
public virtual async Task<T> GetQuery(int id)
{
var connection = await GetConnection();
return (await connection.QueryAsync<T>("SELECT * FROM Event WHERE Id = ?", id))
.FirstOrDefault();
}
}
Edit #1: The problem seems to be related to the fact that my methods are generic. If I change them to specific for the model "connection.Table<EventDataModel>.Where(..." it works. Will generic methods not work?
Edit #2: I added a 'class' constraint onto T to go along with the existing 'IDataModel, new()' constraints, and that seems to have fixed the issue...Does that make sense?