Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Reflection;
- using System.Threading.Tasks;
- namespace System.Data.Services.Client
- {
- public static class AsyncRequests
- {
- public static Task<IEnumerable<T>> WhereAsync<T>(this DataServiceQuery<T> collection, Expression<Func<T, bool>> predicate) where T : class
- {
- var task = new Task<IEnumerable<T>>(
- () =>
- {
- T[] result = null;
- if (predicate != null)
- result = collection.Where(predicate).ToArray();
- else
- result = collection.ToArray();
- return result;
- }
- );
- task.Start();
- return task;
- }
- public static Task<T> FirstOrDefaultAsync<T>(this DataServiceQuery<T> collection, Expression<Func<T, bool>> predicate) where T : class
- {
- var task = new Task<T>(
- () =>
- {
- return collection.Where(predicate).FirstOrDefault();
- }
- );
- task.Start();
- return task;
- }
- public static Task<int> CountAsync<T>(this DataServiceQuery<T> collection, Expression<Func<T, bool>> predicate) where T : class
- {
- var task = new Task<int>(
- () =>
- {
- return collection.Count(predicate);
- }
- );
- task.Start();
- return task;
- }
- }
- // http://blogs.msdn.com/b/stuartleeks/archive/2008/09/15/dataservicequery-t-expand.aspx
- public static class DataServiceQueryExtensions
- {
- /// <summary>
- /// Тёплый ламповый типизированный инклуд.
- /// </summary>
- public static DataServiceQuery<TSource> Include<TSource, TPropType>(this DataServiceQuery<TSource> source, Expression<Func<TSource, TPropType>> propertySelector)
- {
- string expandString = BuildString(propertySelector);
- return source.Expand(expandString);
- }
- private static string BuildString(Expression propertySelector)
- {
- switch (propertySelector.NodeType)
- {
- case ExpressionType.Lambda:
- LambdaExpression lambdaExpression = (LambdaExpression)propertySelector;
- return BuildString(lambdaExpression.Body);
- case ExpressionType.Quote:
- UnaryExpression unaryExpression = (UnaryExpression)propertySelector;
- return BuildString(unaryExpression.Operand);
- case ExpressionType.MemberAccess:
- MemberExpression expression = (MemberExpression)propertySelector;
- string resultString = expression.Member.Name;
- string expressionString = BuildString(expression.Expression);
- if (!string.IsNullOrEmpty(expressionString))
- resultString = expressionString + "/" + resultString;
- return resultString;
- case ExpressionType.Call:
- MethodCallExpression methodCallExpression = (MethodCallExpression)propertySelector;
- if (IsSelect(methodCallExpression.Method))
- return BuildString(methodCallExpression.Arguments[0]) + "/" + BuildString(methodCallExpression.Arguments[1]);
- break;
- case ExpressionType.Parameter:
- return null;
- }
- throw new InvalidOperationException("Expression must be a member expression or an Select call: " + propertySelector.ToString());
- }
- private static readonly MethodInfo[] SelectMethods;
- static DataServiceQueryExtensions()
- {
- Type type = typeof(System.Linq.Enumerable);
- SelectMethods = type.GetMethods().Where(mi => mi.Name == "Select").ToArray();
- }
- private static bool IsSelect(MethodInfo methodInfo)
- {
- if (methodInfo.IsGenericMethod)
- {
- if (!methodInfo.IsGenericMethodDefinition)
- {
- methodInfo = methodInfo.GetGenericMethodDefinition();
- }
- }
- return SelectMethods.Contains(methodInfo);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement