Beispiel #1
0
        public TResult Execute <TResult>(Expression expression)
        {
            var result = NemoQueryContext.Execute(expression, _connection);

            if (typeof(IEnumerable).IsAssignableFrom(typeof(TResult)))
            {
                return((TResult)result);
            }
            return(((IEnumerable)result).OfType <TResult>().FirstOrDefault());
        }
Beispiel #2
0
        public TResult Execute <TResult>(Expression expression)
        {
            var result   = NemoQueryContext.Execute(expression, _connection, config: _config);
            var typeName = result.GetType().Name;

            if (typeName == "EagerLoadEnumerable`1" && !typeof(IEnumerable).IsAssignableFrom(typeof(TResult)))
            {
                return(((IEnumerable)result).OfType <TResult>().FirstOrDefault());
            }
            return((TResult)result);
        }
Beispiel #3
0
        public async ValueTask <TResult> ExecuteAsync <TResult>(Expression expression, CancellationToken token)
        {
            var async    = NemoQueryContext.Execute(expression, _connection, true, _config);
            var typeName = async.GetType().Name;

            if (typeof(IEnumerable).IsAssignableFrom(typeof(TResult)))
            {
                var type = Reflector.GetElementType(typeof(TResult));
                if (typeof(IList).IsAssignableFrom(typeof(TResult)))
                {
                    var task = (Task)ToEnumerableAsyncMethod.MakeGenericMethod(type).Invoke(null, new object[] { async });
                    await task.ConfigureAwait(false);

                    var items = (IEnumerable)typeof(Task <>).MakeGenericType(typeof(IEnumerable <>).MakeGenericType(type)).GetProperty("Result").GetGetMethod().Invoke(task, null);
                    var list  = List.Create(type);
                    foreach (var item in items)
                    {
                        list.Add(item);
                    }

                    if (typeof(TResult).IsArray)
                    {
                        return((TResult)(object)List.CreateArray(type, list));
                    }
                    return((TResult)list);
                }
                else
                {
                    var task = (Task <TResult>)ToEnumerableAsyncMethod.MakeGenericMethod(type).Invoke(null, new object[] { async });
                    return(await task.ConfigureAwait(false));
                }
            }
            else if (typeName == "EagerLoadEnumerableAsync`1" && !typeof(IEnumerable).IsAssignableFrom(typeof(TResult)))
            {
                return(await((IAsyncEnumerable <TResult>)async).FirstOrDefaultAsync().ConfigureAwait(false));
            }
            else
            {
                return(await((Task <TResult>)async).ConfigureAwait(false));
            }
        }
Beispiel #4
0
 public object Execute(Expression expression)
 {
     return(NemoQueryContext.Execute(expression, _connection));
 }