/// <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()); case SequenceMethod.LongCount: case SequenceMethod.Count: return(((DataServiceQuery <TElement>)query).GetValue(this.Context, ParseQuerySetCount <TElement>)); case SequenceMethod.SumIntSelector: case SequenceMethod.SumDoubleSelector: case SequenceMethod.SumDecimalSelector: case SequenceMethod.SumLongSelector: case SequenceMethod.SumSingleSelector: case SequenceMethod.SumNullableIntSelector: case SequenceMethod.SumNullableDoubleSelector: case SequenceMethod.SumNullableDecimalSelector: case SequenceMethod.SumNullableLongSelector: case SequenceMethod.SumNullableSingleSelector: case SequenceMethod.AverageIntSelector: case SequenceMethod.AverageDoubleSelector: case SequenceMethod.AverageDecimalSelector: case SequenceMethod.AverageLongSelector: case SequenceMethod.AverageSingleSelector: case SequenceMethod.AverageNullableIntSelector: case SequenceMethod.AverageNullableDoubleSelector: case SequenceMethod.AverageNullableDecimalSelector: case SequenceMethod.AverageNullableLongSelector: case SequenceMethod.AverageNullableSingleSelector: case SequenceMethod.MinSelector: // Mapped to a generic expression - Min(IQueryable`1<T0>, Expression`1<Func`2<T0, T1>>)->T1 case SequenceMethod.MaxSelector: // Mapped to a generic expression - Max(IQueryable`1<T0>, Expression`1<Func`2<T0, T1>>)->T1 case SequenceMethod.CountDistinctSelector: // Mapped to a generic expression - CountDistinct(IQueryable`1<T0>, Expression`1<Func`2<T0, T1>>)->Int32 return(((DataServiceQuery <TElement>)query).GetValue(this.Context, this.ParseAggregateSingletonResult <TElement>)); 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); }