Exemple #1
0
        /// <summary>
        /// Translates the specified linq expression into a <c ref="LinqQueryDescription" />.
        /// </summary>
        /// <param name="expression">The linq expression.</param>
        /// <returns></returns>
        internal LinqQueryDescription Translate(Expression expression)
        {
            _query = new LinqQueryDescription();

            if (expression.Type.IsGenericType)
            {
                _query.ElementType = expression.Type.GetGenericArguments()[0];
            }

            Visit(expression);

            return(_query);
        }
Exemple #2
0
        public override object Execute(Expression expression)
        {
            LinqQueryDescription queryDescription = Translate(expression);

            // Call the API..
            string xml = _proxy.FindElements(queryDescription);

            Response response = ModelSerializer.DeserializeTo <Response>(xml);


            // Guard against an empty response..
            IModelList elementCollection = (response == null)
                ? (IModelList)Activator.CreateInstance(queryDescription.ElementListType)        // TODO: too much going on here, needs tidying up
                : response.GetTypedProperty(queryDescription.ElementListType);


            if (queryDescription.ClientSideExpression == null)
            {
                return(elementCollection);
            }

            switch (queryDescription.ClientSideExpression)
            {
            case "FirstOrDefault":

                return(elementCollection.Count == 0
                        ? null
                        : elementCollection[0]);

            case "First":

                if (elementCollection.Count == 0)
                {
                    throw new InvalidOperationException("The ModelList contains no items");
                }

                return(elementCollection[0]);

            case "SingleOrDefault":

                if (elementCollection.Count > 1)
                {
                    throw new InvalidOperationException("The ModelList contains no items");
                }

                if (elementCollection.Count == 0)
                {
                    return(null);
                }

                return(elementCollection[0]);

            case "Single":

                if (elementCollection.Count != 1)
                {
                    throw new InvalidOperationException("The ModelList contains no items");
                }

                return(elementCollection[0]);

            case "Count":

                return(elementCollection.Count);

            default:

                throw new NotImplementedException(string.Format("The client side aggregator {0} cannot currently be performed", queryDescription.ClientSideExpression));
            }
        }