Пример #1
0
        protected DataServiceQueryable(IEntitySet <TBase> entitySet, DataServiceQueryable parent)
            : base(parent)
        {
            this.EntitySet = entitySet;

            Initialize();
        }
 internal DataServiceQueryableImp(DataServiceQueryable <TEntity, TBase> source, IEnumerable <TBase> enumerable)
     : base(source.EntitySet, source)
 {
     this._enumerable = enumerable;
 }
Пример #3
0
 protected DataServiceQueryable(DataServiceQueryable parent)
 {
     this.Parent = parent;
     Initialize();
 }
Пример #4
0
        private void LoadData(IEntityCollectionView <T> view)
        {
            #region query

            var requestId = ++RequestId;

            var query = Queryable.IncludeTotalCount();

            #endregion query

            #region paging

            if (view.PageSize > 0)
            {
                query = query
                        .Skip(view.PageIndex * view.PageSize)
                        .Take(view.PageSize);
            }

            #endregion pagging

            #region filters

            foreach (var filterExpression in view.FilterExpressions)
            {
                var remoteExpression = DataServiceQueryable.ToRemoteExpression(filterExpression);
                query = query.Where(remoteExpression);
            }

            #endregion filters

            #region sorting

            // apply sort descriptors
            IOrderedDataServiceQueryable <T, T> sortedQuery = null;
            foreach (var sortDescription in view.SortDescriptions)
            {
                var sortExpression = sortDescription.ToExpression <T>();
                if (ReferenceEquals(sortedQuery, null))
                {
                    sortedQuery = query.OrderBy(sortExpression);
                }
                else
                {
                    sortedQuery = sortedQuery.ThenBy(sortExpression);
                }
            }
            if (!ReferenceEquals(sortedQuery, null))
            {
                query = sortedQuery;
            }

            #endregion sorting

            #region request

            // notify data loading
            OnDataLoading();

            query.ExecuteAsync(
                (result) =>
            {
                if (requestId < RequestId)
                {
                    // request outdated
                    return;
                }

                // synchronize with main thread
                Dispatcher.BeginInvoke((Action) delegate
                {
                    // check for exception
                    if (result.IsFaulted)
                    {
                        var queryException = QueryException;
                        if (queryException != null)
                        {
                            queryException(this, new ExceptionEventArgs(result.Exception));
                            OnDataLoaded(null);
                            return;
                        }
                        else
                        {
                            throw new Exception("Query failed", result.Exception);
                        }
                    }

                    // retrieve data
                    var entitySet = result.EntitySet;

                    // fill items
                    view.Clear();
                    foreach (var item in entitySet)
                    {
                        view.Add(item);
                    }

                    // sets total count
                    view.TotalItemCount = entitySet.TotalCount.HasValue ? (int)entitySet.TotalCount.Value : -1;

                    // notify data loaded
                    OnDataLoaded(entitySet);
                });
            }
                );

            #endregion request
        }
Пример #5
0
 internal DataServiceQueryableImp(IEntitySet <TBase> source, DataServiceQueryable parent)
     : base(source, parent)
 {
 }
Пример #6
0
 internal DataServiceQueryableImp(DataServiceQueryable <TEntity, TBase> source)
     : base(source.EntitySet, source)
 {
 }
Пример #7
0
        private bool ParseExpression(MethodCallExpression expression)
        {
            switch (expression.Method.Name)
            {
            case "Where":
            {
                var exp = (Expression <Func <TEntity, bool> >)((UnaryExpression)expression.Arguments[1]).Operand;
                _queriable = (DataServiceQueryable <TEntity, TBase>)_queriable.Where(exp);
            }
            break;

            case "First":
            {
                if (expression.Arguments.Count > 1)
                {
                    var exp = (Expression <Func <TEntity, bool> >)((UnaryExpression)expression.Arguments[1]).Operand;
                    _queriable = new DataServiceQueryableImp <TEntity, TBase>(_queriable, new[] { _queriable.First(exp) });
                }
                else
                {
                    _queriable = new DataServiceQueryableImp <TEntity, TBase>(_queriable, new[] { _queriable.First() });
                }
            }
            break;

            case "FirstOrDefault":
            {
                if (expression.Arguments.Count > 1)
                {
                    var exp = (Expression <Func <TEntity, bool> >)((UnaryExpression)expression.Arguments[1]).Operand;
                    _queriable = new DataServiceQueryableImp <TEntity, TBase>(_queriable, new[] { _queriable.FirstOrDefault(exp) });
                }
                else
                {
                    _queriable = new DataServiceQueryableImp <TEntity, TBase>(_queriable, new[] { _queriable.FirstOrDefault() });
                }
            }
            break;

            case "Single":
            {
                if (expression.Arguments.Count > 1)
                {
                    var exp = (Expression <Func <TEntity, bool> >)((UnaryExpression)expression.Arguments[1]).Operand;
                    _queriable = new DataServiceQueryableImp <TEntity, TBase>(_queriable, new[] { _queriable.Single(exp) });
                }
                else
                {
                    _queriable = new DataServiceQueryableImp <TEntity, TBase>(_queriable, new[] { _queriable.Single() });
                }
            }
            break;

            case "SingleOrDefault":
            {
                if (expression.Arguments.Count > 1)
                {
                    var exp = (Expression <Func <TEntity, bool> >)((UnaryExpression)expression.Arguments[1]).Operand;
                    _queriable = new DataServiceQueryableImp <TEntity, TBase>(_queriable, new[] { _queriable.SingleOrDefault(exp) });
                }
                else
                {
                    _queriable = new DataServiceQueryableImp <TEntity, TBase>(_queriable, new[] { _queriable.SingleOrDefault() });
                }
            }
            break;

            case "OrderBy":
            {
                var exp = (UnaryExpression)expression.Arguments[1];
                _queriable = (DataServiceQueryable <TEntity, TBase>)_queriable.OrderBy((LambdaExpression)exp.Operand);
            }
            break;

            case "OrderByDescending":
            {
                var exp = (UnaryExpression)expression.Arguments[1];
                _queriable = (DataServiceQueryable <TEntity, TBase>)_queriable.OrderByDescending((LambdaExpression)exp.Operand);
            }
            break;

            case "ThenBy":
            {
                var exp = (UnaryExpression)expression.Arguments[1];
                _queriable = (DataServiceQueryable <TEntity, TBase>)((OrderedDataServiceQueryable <TEntity, TBase>)_queriable).ThenBy((LambdaExpression)exp.Operand);
            }
            break;

            case "ThenByDescending":
            {
                var exp = (UnaryExpression)expression.Arguments[1];
                _queriable = (DataServiceQueryable <TEntity, TBase>)((OrderedDataServiceQueryable <TEntity, TBase>)_queriable).ThenByDescending((LambdaExpression)exp.Operand);
            }
            break;

            case "Skip":
            {
                var exp = (ConstantExpression)expression.Arguments[1];
                _queriable = (DataServiceQueryable <TEntity, TBase>)_queriable.Skip((int)exp.Value);
            }
            break;

            case "Take":
            {
                var exp = (ConstantExpression)expression.Arguments[1];
                _queriable = (DataServiceQueryable <TEntity, TBase>)_queriable.Take((int)exp.Value);
            }
            break;

            case "Count":
            {
                if (expression.Arguments.Count == 2)
                {
                    var exp = (Expression <Func <TEntity, bool> >)((UnaryExpression)expression.Arguments[1]).Operand;
                    _totalCountInt32 = _queriable.Count(exp);
                }
                else
                {
                    _totalCountInt32 = _queriable.Count();
                }
            }
            break;

            case "LongCount":
            {
                if (expression.Arguments.Count == 2)
                {
                    var exp = (Expression <Func <TEntity, bool> >)((UnaryExpression)expression.Arguments[1]).Operand;
                    _totalCountInt64 = _queriable.LongCount(exp);
                }
                else
                {
                    _totalCountInt64 = _queriable.LongCount();
                }
            }
            break;

            default:
                return(false);
            }

            return(true);
        }
Пример #8
0
 internal QueryProvider(DataServiceQueryable <TEntity, TBase> queriable)
 {
     _queriable = queriable;
 }
Пример #9
0
 internal OrderedDataServiceQueryable(DataServiceQueryable <TEntity, TBase> queryable)
     : base(queryable.EntitySet, queryable)
 {
     _queryable = queryable;
 }