Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

NotSupportedException in CompileExpr #168

Open
oansquer opened this Issue Feb 25, 2013 · 0 comments

Comments

1 participant

Hi,

I want to create a generic method to get an element by its primary key with this code :

public interface IBusinessEntity
{
    int ID { get; set; }
}

public class MySQLiteConnection : SQLiteConnection
{
    private static MySQLiteConnection Instance
    {
        get;
        set;
    }

    ...

    public static T GetItem<T>(int id) where T : IBusinessEntity, new()
    {
        lock (locker)
        {
            return Instance.Table<T>().Where(x => x.ID == id).FirstOrDefault();
        }
    }

    ...
}

But the generic GetItem method fails with NotSupportedException exception.
To make it work, I have modified CompileExpr method :

            ...
            else if (expr.NodeType == ExpressionType.MemberAccess) {
            var mem = (MemberExpression)expr;
            // START ============================
            if (mem.Expression.NodeType == ExpressionType.Convert &&
                ((UnaryExpression)mem.Expression).Operand.NodeType == ExpressionType.Parameter)
            {
                //
                // This is a column of our table, output just the column name
                // Need to translate it if that column name is mapped
                //
                var columnName = Table.FindColumnWithPropertyName(mem.Member.Name).Name;
                return new CompileResult { CommandText = "\"" + columnName + "\"" };
            } else
            // END ==================================
            if (mem.Expression.NodeType == ExpressionType.Parameter) {
            ...

I'm not sure this is the right change, but is it possible to make a correction in a future version?

Best regards,

Olivier Ansquer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment