internal override Expression VisitMethodCall(MethodCallExpression m)
        {
            if (m.Method.DeclaringType == typeof(Queryable))
            {
                if (m.Method.Name == "Where")
                {
                    this.Visit(m.Arguments[0]);
                    LambdaExpression lambda = (LambdaExpression)StripQuotes(m.Arguments[1]);
                    this.Visit(lambda.Body);
                    return(m);
                }
                if (m.Method.Name == "OrderBy")
                {
                    this.Visit(m.Arguments[0]);
                    AddSort((LambdaExpression)StripQuotes(m.Arguments[1]), true, true);
                    return(m);
                }
                if (m.Method.Name == "OrderByDescending")
                {
                    this.Visit(m.Arguments[0]);
                    AddSort((LambdaExpression)StripQuotes(m.Arguments[1]), true, false);
                    return(m);
                }
                if (m.Method.Name == "ThenBy")
                {
                    this.Visit(m.Arguments[0]);
                    AddSort((LambdaExpression)StripQuotes(m.Arguments[1]), false, true);
                    return(m);
                }
                if (m.Method.Name == "ThenByDescending")
                {
                    this.Visit(m.Arguments[0]);
                    AddSort((LambdaExpression)StripQuotes(m.Arguments[1]), false, false);
                    return(m);
                }
                if (m.Method.Name == "Count")
                {
                    RecurseToWhereOrRunLambda(m);
                    int foundCount = (int)NativeQuery.count(_coreQueryHandle);
                    return(Expression.Constant(foundCount));
                }
                if (m.Method.Name == "Any")
                {
                    RecurseToWhereOrRunLambda(m);
                    var  rowPtr   = NativeQuery.findDirect(_coreQueryHandle, IntPtr.Zero);
                    var  firstRow = Realm.CreateRowHandle(rowPtr, _realm.SharedRealmHandle);
                    bool foundAny = !firstRow.IsInvalid;
                    return(Expression.Constant(foundAny));
                }
                if (m.Method.Name == "First")
                {
                    RecurseToWhereOrRunLambda(m);
                    RowHandle firstRow = null;
                    if (_optionalSortOrderHandle == null)
                    {
                        var rowPtr = NativeQuery.findDirect(_coreQueryHandle, IntPtr.Zero);
                        firstRow = Realm.CreateRowHandle(rowPtr, _realm.SharedRealmHandle);
                    }
                    else
                    {
                        using (ResultsHandle rh = _realm.MakeResultsForQuery(_retType, _coreQueryHandle, _optionalSortOrderHandle))
                        {
                            var rowPtr = NativeResults.get_row(rh, IntPtr.Zero);
                            firstRow = Realm.CreateRowHandle(rowPtr, _realm.SharedRealmHandle);
                        }
                    }
                    if (firstRow == null || firstRow.IsInvalid)
                    {
                        throw new InvalidOperationException("Sequence contains no matching element");
                    }
                    return(Expression.Constant(_realm.MakeObjectForRow(_retType, firstRow)));
                }
                if (m.Method.Name == "Single")  // same as unsorted First with extra checks
                {
                    RecurseToWhereOrRunLambda(m);
                    var rowPtr   = NativeQuery.findDirect(_coreQueryHandle, IntPtr.Zero);
                    var firstRow = Realm.CreateRowHandle(rowPtr, _realm.SharedRealmHandle);
                    if (firstRow.IsInvalid)
                    {
                        throw new InvalidOperationException("Sequence contains no matching element");
                    }
                    IntPtr nextIndex  = (IntPtr)(firstRow.RowIndex + 1);
                    var    nextRowPtr = NativeQuery.findDirect(_coreQueryHandle, nextIndex);
                    var    nextRow    = Realm.CreateRowHandle(nextRowPtr, _realm.SharedRealmHandle);
                    if (!nextRow.IsInvalid)
                    {
                        throw new InvalidOperationException("Sequence contains more than one matching element");
                    }
                    return(Expression.Constant(_realm.MakeObjectForRow(_retType, firstRow)));
                }
            }

            if (m.Method.DeclaringType == typeof(string))
            {
                NativeQuery.Operation <string> queryMethod = null;

                if (m.Method == Methods.String.Contains.Value)
                {
                    queryMethod = (q, c, v) => NativeQuery.string_contains(q, c, v, (IntPtr)v.Length);
                }
                else if (m.Method == Methods.String.StartsWith.Value)
                {
                    queryMethod = (q, c, v) => NativeQuery.string_starts_with(q, c, v, (IntPtr)v.Length);
                }
                else if (m.Method == Methods.String.EndsWith.Value)
                {
                    queryMethod = (q, c, v) => NativeQuery.string_ends_with(q, c, v, (IntPtr)v.Length);
                }

                if (queryMethod != null)
                {
                    var member = m.Object as MemberExpression;
                    if (member == null)
                    {
                        throw new NotSupportedException($"The method '{m.Method}' has to be invoked on a RealmObject member");
                    }
                    var columnIndex = NativeQuery.get_column_index(_coreQueryHandle, member.Member.Name, (IntPtr)member.Member.Name.Length);

                    var argument = ExtractConstantValue(m.Arguments.SingleOrDefault());
                    if (argument == null || argument.GetType() != typeof(string))
                    {
                        throw new NotSupportedException($"The method '{m.Method}' has to be invoked with a single string constant argument or closure variable");
                    }
                    queryMethod(_coreQueryHandle, columnIndex, (string)argument);
                    return(m);
                }
            }

            throw new NotSupportedException($"The method '{m.Method.Name}' is not supported");
        }