protected ProcessingResult GetKeys(LambdaExpression keySelector, ProcessingResult parentResult, DataContext context)
        {
            if (!parentResult.IsDeferred())
            {
                var keys             = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(keySelector.ReturnType));
                var compiledSelector = CompiledExpressionStorage.GetOrAddCompiledLambda(keySelector);

                foreach (var item in parentResult.GetItems())
                {
                    keys.Add(compiledSelector.DynamicInvoke(item));
                }

                return(new ProcessingResult(true, keys));
            }

            var nodeReferences = parentResult.GetDeferredList();
            var keysResult     = ExpressionProcessingHelper.ProcessExpression(keySelector.Body, nodeReferences, context);

            if (!keysResult.IsSuccess)
            {
                return(ProcessingResult.Unsuccessful);
            }

            if (keysResult.IsSingleItem)
            {
                keysResult = ExpressionProcessingHelper.CopyInstances(keysResult, nodeReferences.Count,
                                                                      () => ExpressionProcessingHelper.ProcessExpression(keySelector.Body, nodeReferences, context).Result);
            }

            keysResult = new ProcessingResult(true, keysResult.GetLoadedItems(keySelector.ReturnType));
            return(QueryProcessingHelper.NormalizeMultipleResult(keysResult, keySelector.ReturnType));
        }
        public ProcessingResult ProcessQuery(MethodCallExpression query, ProcessingResult parentResult, DataContext context)
        {
            if (!parentResult.IsDeferred())
            {
                return(ProcessingResult.Unsuccessful);
            }

            var unaryExpression  = (UnaryExpression)query.Arguments[1];
            var lambdaExpression = (LambdaExpression)unaryExpression.Operand;

            if (lambdaExpression.Parameters.Count > 1)
            {
                return(ProcessingResult.Unsuccessful);
            }

            var memberExpression = lambdaExpression.Body;
            var nodeReferences   = parentResult.GetDeferredList();

            var result = ExpressionProcessingHelper.ProcessExpression(memberExpression, nodeReferences, context);

            if (!result.IsSuccess)
            {
                return(ProcessingResult.Unsuccessful);
            }

            var targetItemType = QueryProcessingHelper.GetReturnParameterType(query);

            if (!result.IsSingleItem)
            {
                return(QueryProcessingHelper.NormalizeMultipleResult(result, targetItemType));
            }

            var multipleResult = ExpressionProcessingHelper.CopyInstances(result, nodeReferences.Count,
                                                                          () => ExpressionProcessingHelper.ProcessExpression(memberExpression, nodeReferences, context).Result);

            return(new ProcessingResult(true, QueryProcessingHelper.NormalizeMultipleResult(multipleResult, targetItemType).Result));
        }
Esempio n. 3
0
        public ProcessingResult ProcessQuery(MethodCallExpression query, ProcessingResult parentResult, DataContext context)
        {
            if (!parentResult.IsDeferred())
            {
                return(ProcessingResult.Unsuccessful);
            }

            var hasSelector = query.Arguments.Count > 1;
            var elementType = QueryProcessingHelper.GetReturnParameterType(query);

            if (!hasSelector)
            {
                var items = parentResult.GetLoadedItems(elementType);
                return(new ProcessingResult(true, ProcessQuery(items), true));
            }

            var unaryExpression = (UnaryExpression)query.Arguments[1];
            var selectorLambda  = (LambdaExpression)unaryExpression.Operand;

            var selectorResult = ExpressionProcessingHelper.ProcessExpression(selectorLambda.Body, parentResult.GetDeferredList(), context);

            if (!selectorResult.IsSuccess)
            {
                return(ProcessingResult.Unsuccessful);
            }

            if (selectorResult.IsSingleItem)
            {
                var result = Convert.ChangeType(selectorResult.Result, elementType);
                return(new ProcessingResult(true, result, true));
            }

            var loadedItems = selectorResult.GetLoadedItems(elementType);

            return(new ProcessingResult(true, ProcessQuery(loadedItems), true));
        }