Guest User

Untitled

a guest
May 20th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. public static IQueryable<T> SelectExcept<T, TKey>(this IQueryable<T> sequence,
  2. Expression<Func<T, TKey>> excluder) where T : new()
  3. {
  4. List<string> excludedProperties = new List<string>();
  5. if (excluder.Body is MemberExpression memberExpression)
  6. {
  7. excludedProperties.Add(memberExpression.Member.Name);
  8. }
  9. else if (excluder.Body is NewExpression anonymousExpression)
  10. {
  11. excludedProperties.AddRange(anonymousExpression.Members.Select(m => m.Name));
  12. }
  13. var includedProperties = typeof(T).GetProperties()
  14. .Where(p => !excludedProperties.Contains(p.Name));
  15.  
  16. return sequence.Select(x => Selector(x, includedProperties));
  17. }
  18.  
  19. private static T Selector<T>(T obj, IEnumerable<PropertyInfo> properties)
  20. {
  21. var instance = Activator.CreateInstance<T>();
  22. foreach (var property in properties)
  23. property.SetValue(instance, property.GetValue(obj), null);
  24.  
  25. return instance;
  26. }
Add Comment
Please, Sign In to add comment