Пример #1
0
        /// <summary>
        /// Builds the <see cref="QueryDataSet"/> from the specified container data and query queryRepository.
        /// </summary>
        /// <param name="rootDataTypeMap">The collection of rootDataTypes used to build the data set.</param>
        /// <param name="entityContainerData">Entity Container Data</param>
        /// <returns>
        /// Instance of <see cref="QueryDataSet"/> with data populated from the containerData
        /// </returns>
        public virtual IQueryDataSet Build(IDictionary <string, QueryStructuralType> rootDataTypeMap, EntityContainerData entityContainerData)
        {
            QueryDataSet dataSet = new QueryDataSet();

            this.rootDataTypes = rootDataTypeMap;
            this.rowInstances  = new Dictionary <string, Dictionary <EntityDataKey, QueryStructuralValue> >();

            if (null != entityContainerData)
            {
                // phase 1 - build stub QueryStructuralValues for each EntityDataSetRow
                // and create result collections. We're doing it in two phases so that we
                // can set up relationships more easily since all objects
                // are guaranteed to exist.
                foreach (var entitySet in entityContainerData.EntityContainer.EntitySets)
                {
                    var entitySetData = entityContainerData.GetEntitySetData(entitySet.Name);
                    var collection    = this.BuildStubEntities(entitySetData);

                    dataSet.RootQueryData[entitySet.Name] = collection;
                }

                // phase 2 - copy actual data into pre-generated objects
                foreach (var entitySet in entityContainerData.EntityContainer.EntitySets)
                {
                    var entitySetData = entityContainerData.GetEntitySetData(entitySet.Name);

                    this.PopulateObjectInstances(entitySetData);
                }
            }

            return(dataSet);
        }
        /// <summary>
        /// Evaluates the given lambda as if it were called on the given entity. Used for evaluating orderby expressions for the last result in a feed.
        /// </summary>
        /// <param name="entity">The entity to evaluate the lambda for.</param>
        /// <param name="lamda">The lamda to evaluate.</param>
        /// <returns>The result of evaluating the lamda for the given entity.</returns>
        private object EvaluateLambdaForEntity(QueryStructuralValue entity, LinqLambdaExpression lamda)
        {
            var expression = CommonQueryBuilder.Root("fakeSet", entity.Type.CreateCollectionType()).Select(lamda).Single();

            var fakeQueryDataSet = new QueryDataSet();
            fakeQueryDataSet.RootQueryData["fakeSet"] = entity.Type.CreateCollectionType().CreateCollectionWithValues(new[] { entity });

            QueryValue evaluated;
            using (this.ExpressionEvaluator.WithTemporaryDataSet(fakeQueryDataSet))
            {
                evaluated = this.ExpressionEvaluator.Evaluate(expression);
            }

            return ((QueryScalarValue)evaluated).Value;
        }