Last active
August 29, 2015 14:02
-
-
Save OzieWest/2b38c92ab0bd96650e9a to your computer and use it in GitHub Desktop.
Snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public Func<T,T> AsProjections<T>(string fields) where T: class | |
| { | |
| var xParameter = Expression.Parameter(typeof(T), "o"); | |
| var xNew = Expression.New(typeof(T)); | |
| var bindings = fields.Split('.') | |
| .Select(o => o.Trim()) | |
| .Select(o => | |
| { | |
| var mi = typeof(T).GetProperty(o); | |
| var xOriginal = Expression.Property(xParameter, mi); | |
| return Expression.Bind(mi, xOriginal); | |
| } | |
| ); | |
| var xInit = Expression.MemberInit(xNew, bindings); | |
| var lambda = Expression.Lambda<Func<T,T>>(xInit, xParameter); | |
| return lambda.Compile(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment