Exemplo n.º 1
0
        /// <summary>
        /// Converte a expressão da consulta com uma consulta executável.
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        private QueryInfo ConvertToExecutableQuery(Expression query)
        {
            Type source;

            if (!GetSourceEntity(query, out source))
            {
                throw new NotSupportedException("This query expression is not supported!");
            }
            var queryDetails = new QueryInfo()
            {
                Method = _useMethod
            };

            if (_takeTranslator != null)
            {
                queryDetails.TakeParameters = new TakeParameters(_takeTranslator.Count.GetValueOrDefault(), _skipTranslator != null ? _skipTranslator.Skip.GetValueOrDefault() : 0);
            }
            if (_selectTranslator == null || !_selectTranslator.DataMembers.Any())
            {
                _selectTranslator = new ProjectionTranslator(source.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance).Select(f => new Member(null, f)));
            }
            if (!_selectTranslator.DataMembers.Any())
            {
                throw new Exception("There are no items for projection in this query!");
            }
            var projection = new Projection();

            foreach (var i in _selectTranslator.DataMembers)
            {
                projection.Add(new ProjectionEntry(!string.IsNullOrEmpty(i.Owner) ? string.Format("{0}.{1}", i.Owner, i.Info.Name) : i.Info.Name, i.Info.Name));
            }
            queryDetails.Projection = projection;
            IEnumerable <Member> entities = _selectTranslator.DataMembers;

            if (_whereTranslator != null)
            {
                string where = _whereTranslator.WhereClause;
                if (!string.IsNullOrEmpty(where))
                {
                    queryDetails.WhereClause = ConditionalContainer.Parse(where);
                }
            }
            else
            {
                queryDetails.WhereClause = new ConditionalContainer().Add(_parameters);
            }
            var rr = entities.GroupBy(f => f.Owner, f => f).ToArray();

            if (_orderByTranslator != null && _orderByTranslator.Members.Any())
            {
                foreach (var i in _orderByTranslator.Members)
                {
                    if (queryDetails.Sort == null)
                    {
                        queryDetails.Sort = new Sort();
                    }
                    queryDetails.Sort.Add(new SortEntry(new Column(i.DataMember.Name), i.Direction == OrderDirection.Descending));
                }
            }
            return(queryDetails);
        }