public TResult Execute <TResult>(Expression expression) { var objectQuery = CurrentQueryable.GetObjectQuery(); // GET provider var objectQueryProviderProperty = objectQuery.GetType().GetProperty("ObjectQueryProvider", BindingFlags.NonPublic | BindingFlags.Instance); var provider = (IQueryProvider)objectQueryProviderProperty.GetValue(objectQuery); // CREATE query from the expression var createQueryMethod = provider.GetType().GetMethod("CreateQuery", BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { typeof(Expression) }, null); createQueryMethod = createQueryMethod.MakeGenericMethod(typeof(TResult)); var query = createQueryMethod.Invoke(provider, new object[] { expression }); var orderedQuery = query as IOrderedQueryable <TResult>; var queryNew = CurrentQueryable.CreateOrderedQueryable(orderedQuery, false); // EXECUTE the batch queryNew.OwnerBatch.Execute(); // SET result as single value // todo: make something about first or default... queryNew.Result = ((IEnumerable <TResult>)queryNew.Result).FirstOrDefault(); return((TResult)queryNew.Result); }
public IQueryable <TElement> CreateQuery <TElement>(Expression expression) { var query = OriginalProvider.CreateQuery <TElement>(expression); return(CurrentQueryable.CreateOrderedQueryable(query as IOrderedQueryable <TElement>)); }