コード例 #1
0
        internal TElement ReturnSingleton <TElement>(Expression expression)
        {
            SequenceMethod        method;
            IQueryable <TElement> source = new DataServiceQuery <TElement> .DataServiceOrderedQuery(expression, this);

            MethodCallExpression m = expression as MethodCallExpression;

            if (!ReflectionUtil.TryIdentifySequenceMethod(m.Method, out method))
            {
                throw System.Data.Services.Client.Error.MethodNotSupported(m);
            }
            switch (method)
            {
            case SequenceMethod.First:
                return(source.AsEnumerable <TElement>().First <TElement>());

            case SequenceMethod.FirstOrDefault:
                return(source.AsEnumerable <TElement>().FirstOrDefault <TElement>());

            case SequenceMethod.Single:
                return(source.AsEnumerable <TElement>().Single <TElement>());

            case SequenceMethod.SingleOrDefault:
                return(source.AsEnumerable <TElement>().SingleOrDefault <TElement>());

            case SequenceMethod.Count:
            case SequenceMethod.LongCount:
                return((TElement)Convert.ChangeType(((DataServiceQuery <TElement>)source).GetQuerySetCount(this.Context), typeof(TElement), CultureInfo.InvariantCulture.NumberFormat));
            }
            throw System.Data.Services.Client.Error.MethodNotSupported(m);
        }
コード例 #2
0
        /// <summary>Creates and executes a DataServiceQuery based on the passed in expression which results a single value</summary>
        /// <typeparam name="TElement">generic type</typeparam>
        /// <param name="expression">The expression for the new query</param>
        /// <returns>single valued results</returns>
        internal TElement ReturnSingleton <TElement>(Expression expression)
        {
            IQueryable <TElement> query = new DataServiceQuery <TElement> .DataServiceOrderedQuery(expression, this);

            MethodCallExpression mce = expression as MethodCallExpression;

            Debug.Assert(mce != null, "mce != null");

            SequenceMethod sequenceMethod;

            if (ReflectionUtil.TryIdentifySequenceMethod(mce.Method, out sequenceMethod))
            {
                switch (sequenceMethod)
                {
                case SequenceMethod.Single:
                    return(query.AsEnumerable().Single());

                case SequenceMethod.SingleOrDefault:
                    return(query.AsEnumerable().SingleOrDefault());

                case SequenceMethod.First:
                    return(query.AsEnumerable().First());

                case SequenceMethod.FirstOrDefault:
                    return(query.AsEnumerable().FirstOrDefault());

#if !ASTORIA_LIGHT
                case SequenceMethod.LongCount:
                case SequenceMethod.Count:
                    return((TElement)Convert.ChangeType(((DataServiceQuery <TElement>)query).GetQuerySetCount(this.Context), typeof(TElement), System.Globalization.CultureInfo.InvariantCulture.NumberFormat));
#endif
                default:
                    throw Error.MethodNotSupported(mce);
                }
            }

            // Should never get here - should be caught by expression compiler.
            Debug.Assert(false, "Not supported singleton operator not caught by Resource Binder");
            throw Error.MethodNotSupported(mce);
        }