Example #1
0
        /// <summary>
        /// <see cref="ODataQueryCriteria"/> 查询的数据层实现。
        /// </summary>
        /// <param name="criteria"></param>
        public virtual EntityList GetBy(ODataQueryCriteria criteria)
        {
            var f = QueryFactory.Instance;
            var t = f.Table(this.Repository);

            var q = f.Query(from: t);

            var properties = this.Repository.EntityMeta.ManagedProperties.GetCompiledProperties();

            //filter
            if (!string.IsNullOrWhiteSpace(criteria.Filter))
            {
                var parser = new ODataFilterParser
                {
                    _mainTable  = t,
                    _properties = properties
                };
                q.Where = parser.Parse(criteria.Filter);
            }

            //order by
            if (!string.IsNullOrWhiteSpace(criteria.OrderBy))
            {
                var orderByProperties = criteria.OrderBy.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var orderByExp in orderByProperties)
                {
                    var values   = orderByExp.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    var property = values[0];
                    var orderBy  = properties.Find(property, true);
                    if (orderBy != null)
                    {
                        var dir = values.Length == 1 || values[1].ToLower() == "asc" ? OrderDirection.Ascending : OrderDirection.Descending;
                        q.OrderBy.Add(f.OrderBy(t.Column(orderBy), dir));
                    }
                }
            }

            //expand
            if (!string.IsNullOrWhiteSpace(criteria.Expand))
            {
                criteria.EagerLoad = new EagerLoadOptions();

                var expandProperties = criteria.Expand.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var expand in expandProperties)
                {
                    var mp = properties.Find(expand, true);
                    if (mp != null)
                    {
                        if (mp is IListProperty)
                        {
                            criteria.EagerLoad.LoadWith(mp as IListProperty);
                        }
                        else if (mp is IRefEntityProperty)
                        {
                            criteria.EagerLoad.LoadWith(mp as IRefEntityProperty);
                        }
                    }
                }
            }

            return(this.QueryList(q, criteria.PagingInfo, criteria.EagerLoad));
        }
Example #2
0
        /// <summary>
        /// <see cref="ODataQueryCriteria"/> 查询的数据层实现。
        /// </summary>
        /// <param name="criteria"></param>
        public virtual EntityList GetBy(ODataQueryCriteria criteria)
        {
            var t = f.Table(this.Repository);

            var q = f.Query(from: t);

            var properties = this.Repository.EntityMeta.ManagedProperties.GetCompiledProperties();

            #region Filter

            if (!string.IsNullOrWhiteSpace(criteria.Filter))
            {
                var parser = new ODataFilterParser
                {
                    _properties = properties
                };
                parser.Parse(criteria.Filter, q);
            }

            #endregion

            #region OrderBy

            if (!string.IsNullOrWhiteSpace(criteria.OrderBy))
            {
                var orderByProperties = criteria.OrderBy.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var orderByExp in orderByProperties)
                {
                    var values   = orderByExp.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    var property = values[0];
                    var orderBy  = properties.Find(property, true);
                    if (orderBy != null)
                    {
                        var dir = values.Length == 1 || values[1].ToLower() == "asc" ? OrderDirection.Ascending : OrderDirection.Descending;
                        q.OrderBy.Add(f.OrderBy(t.Column(orderBy), dir));
                    }
                }
            }

            #endregion

            #region Expand

            if (!string.IsNullOrWhiteSpace(criteria.Expand))
            {
                var eagerLoad = new EagerLoadOptions();

                var expandProperties = criteria.Expand.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                var splitter         = new char[] { '.' };
                foreach (var expand in expandProperties)
                {
                    //如果有'.',表示类似于 Section.Chapter.Book 这种表达式。
                    if (expand.Contains('.'))
                    {
                        Type nextEntityType = null;//下一个需要使用的实体类型

                        var cascadeProperties = expand.Split(splitter, StringSplitOptions.RemoveEmptyEntries);
                        foreach (var property in cascadeProperties)
                        {
                            var container = properties;
                            if (nextEntityType != null)
                            {
                                var meta = CommonModel.Entities.Find(nextEntityType);
                                container = meta.ManagedProperties.GetCompiledProperties();
                            }

                            var mp = container.Find(property, true);
                            if (mp != null)
                            {
                                var refProperty = mp as IRefEntityProperty;
                                if (refProperty != null)
                                {
                                    eagerLoad.LoadWith(refProperty);
                                    nextEntityType = refProperty.RefEntityType;
                                }
                                else if (mp is IListProperty)
                                {
                                    eagerLoad.LoadWith(mp as IListProperty);
                                    nextEntityType = (mp as IListProperty).ListEntityType;
                                }
                            }
                        }
                    }
                    else
                    {
                        var mp = properties.Find(expand, true);
                        if (mp != null)
                        {
                            if (mp is IListProperty)
                            {
                                eagerLoad.LoadWith(mp as IListProperty);
                            }
                            else if (mp is IRefEntityProperty)
                            {
                                eagerLoad.LoadWith(mp as IRefEntityProperty);
                            }
                        }
                        else if (expand == EntityConvention.TreeChildrenPropertyName)
                        {
                            eagerLoad.LoadWithTreeChildren();
                        }
                    }

                    criteria.EagerLoad = eagerLoad;
                }
            }

            #endregion

            return(this.QueryList(q, criteria.PagingInfo, criteria.EagerLoad));
        }
Example #3
0
        private static IQuery Parse(string filter, IRepository repo)
        {
            var f = QueryFactory.Instance;
            //var t = f.Table(repo, "T0");
            var q = f.Query(repo);
            var parser = new ODataFilterParser
            {
                _properties = repo.EntityMeta.ManagedProperties.GetCompiledProperties()
            };

            parser.Parse(filter, q);
            return q;
        }