Ejemplo n.º 1
0
        internal static TResult MapToType <TResult>(IEnumerable <DynamicObject> dataRecords, IDynamicObjectMapper mapper, Expression expression)
        {
            var elementType = TypeHelper.GetElementType(typeof(TResult));

            if (mapper is null)
            {
                mapper = new DynamicQueryResultMapper();
            }

            if (dataRecords is null)
            {
                return(default);
Ejemplo n.º 2
0
        internal static T MapToType <T>(IEnumerable <DynamicObject> dataRecords, IDynamicObjectMapper mapper, Expression expression)
        {
            var elementType    = TypeHelper.GetElementType(typeof(T));
            var strElementType = elementType.Name;

            if (mapper is null)
            {
                mapper = new DynamicQueryResultMapper();
            }

            var result = mapper.Map(dataRecords, elementType);

            if (result is null)
            {
                return(default(T));
            }

            if (typeof(T).IsAssignableFrom(typeof(IEnumerable <>).MakeGenericType(elementType)))
            {
                return((T)result);
            }

            if (typeof(T).IsAssignableFrom(elementType))
            {
                try
                {
                    object single;
                    if ((expression as MethodCallExpression)?.Arguments.Count == 2)
                    {
                        var predicate = GetTruePredicate(elementType);
                        single = MethodInfos.Enumerable.SingleWithFilter.MakeGenericMethod(elementType).Invoke(null, new object[] { result, predicate });
                    }
                    else
                    {
                        single = MethodInfos.Enumerable.Single.MakeGenericMethod(elementType).Invoke(null, new object[] { result });
                    }

                    return((T)single);
                }
                catch (TargetInvocationException ex)
                {
                    throw ex.InnerException;
                }
            }

            throw new Exception(string.Format("Failed to cast result of type '{0}' to '{1}'", result.GetType(), typeof(T)));
        }