A translator from LINQ expression queries to Mongo queries.
예제 #1
0
        /// <summary>
        /// Executes a query. Calls the generic method Execute{{T}} to actually execute the query.
        /// </summary>
        /// <param name="expression">The query expression.</param>
        /// <returns>The result of the query.</returns>
        public object Execute(Expression expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }

            var translatedQuery = MongoQueryTranslator.Translate(this, expression);

            return(translatedQuery.Execute());
        }
예제 #2
0
        /// <summary>
        /// Gets an enumerator by executing the query.
        /// </summary>
        /// <typeparam name="T">Type element type.</typeparam>
        /// <param name="expression">The LINQ expression.</param>
        /// <returns>An enumerator for the results of the query.</returns>
        public IEnumerator <T> GetEnumerator <T>(Expression expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }
            if (!typeof(IEnumerable <T>).IsAssignableFrom(expression.Type))
            {
                throw new ArgumentException("Argument expression is not valid.");
            }
            var translatedQuery = MongoQueryTranslator.Translate(_collection, expression);

            return(translatedQuery.GetEnumerator <T>());
        }
예제 #3
0
        /// <summary>
        /// Executes a query.
        /// </summary>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <param name="expression">The query expression.</param>
        /// <returns>The result of the query.</returns>
        public TResult Execute <TResult>(Expression expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }
            if (!typeof(TResult).IsAssignableFrom(expression.Type))
            {
                throw new ArgumentException("Argument expression is not valid.");
            }
            var translatedQuery = MongoQueryTranslator.Translate(_collection, expression);

            return((TResult)translatedQuery.Execute());
        }
예제 #4
0
        // public methods
        /// <summary>
        /// Builds the MongoDB query that will be sent to the server when the LINQ query is executed.
        /// </summary>
        /// <typeparam name="T">The type of the documents being queried.</typeparam>
        /// <param name="query">The LINQ query.</param>
        /// <returns>The MongoDB query.</returns>
        public IMongoQuery BuildMongoQuery <T>(MongoQueryable <T> query)
        {
            var translatedQuery = MongoQueryTranslator.Translate(this, ((IQueryable)query).Expression);

            return(((SelectQuery)translatedQuery).BuildQuery());
        }
예제 #5
0
        /// <summary>
        /// Gets a string representation of the LINQ expression translated to a MongoDB query.
        /// </summary>
        /// <param name="expression">The LINQ expression.</param>
        /// <returns>A string.</returns>
        public string GetQueryText(Expression expression)
        {
            var translatedQuery = MongoQueryTranslator.Translate(_collection, expression);

            return(translatedQuery.ToString());
        }