Exemplo n.º 1
0
        public Func <TSource, TResult> CreateSelector <TSource, TResult>()
        {
            // if we are selecting * then result type should equal source type
            if (AllColumns)
            {
                return(ExpressionFactory.CreateIdentitySelector <TSource, TResult>().Compile());
            }

            // if selecting only one field create a simple selection function
            IEnumerable <string> fields = Columns.GetFieldList();

            if (fields.Count() == 1)
            {
                return(ExpressionFactory.CreateSinglePropertySelector <TSource, TResult>(fields.First()).Compile());
            }

            // if TResult is dynamic return an expando object
            if (typeof(TResult) == typeof(object))
            {
                return(ExpressionFactory.CreateExpandoSelector <TSource, TResult>(fields, Columns.GetResultList()).Compile());
            }

            // if return type has an empty constructor use it and memberinit
            if (typeof(TResult).HasEmptyConstructor())
            {
                return(ExpressionFactory.CreateMemberInitSelector <TSource, TResult>(fields, Columns.GetResultList()).Compile());
            }

            // otherwise try and use a constructor that matches the field list types
            Type[] resultTypes = typeof(TSource).GetPropertyOrFieldTypes(fields);
            return(ExpressionFactory.CreateConstructorCallSelector <TSource, TResult>(fields, resultTypes).Compile());
        }