score:2

Accepted answer

You can do it like this;

    public static Func<T, T> GetExpression<T>(string propertyNames)
    {
        var xParameter = Expression.Parameter(typeof(T), "parameter");

        var xNew = Expression.New(typeof(T));

        var selectFields = propertyNames.Split(',').Select(parameter => parameter.Trim())
            .Select(parameter => {

                    var prop = typeof(T).GetProperty(parameter);

                    if (prop == null) // The field doesn't exist
                    {
                        return null;
                    }
                    var xOriginal = Expression.Property(xParameter, prop);

                    return Expression.Bind(prop, xOriginal);
                }
            ).Where(x => x != null);

        var lambda = Expression.Lambda<Func<T, T>>(Expression.MemberInit(xNew, selectFields), xParameter);

        return lambda.Compile();
    }

Usage;

    var list = new List<Question>{new Question{QuestionEnglish = "QuestionName",QuestionId = 1}};
    var result = list.Select(GetExpression<Question>("QuestionId, QuestionEnglish"));

Related Query

More Query from same tag