code_junkie

Generate lambda with Reflection Info

Nov 12th, 2011
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. model.GetTable<T>().Where(t => here equality );
  2.  
  3. public static T GetById(Guid id)
  4. {
  5. Type entType = typeof(T);
  6.  
  7. if (!CheckTable(entType)) {
  8. throw new TypeLoadException(string.Format(
  9. "{0} is not Table Entity, has no attribute Table", entType.FullName));
  10. }
  11.  
  12. string property = GetPrimaryKeyName(entType).Name;
  13.  
  14. ParameterExpression cs;
  15. var lambda = Expression.Lambda<Func<Personal, bool>>(
  16. Expression.Equal(
  17. Expression.Property(
  18. cs = Expression.Parameter(typeof(T), "p"),
  19. entType.GetProperty(property).GetGetMethod()
  20. ),
  21. Expression.Constant(id),
  22. false,
  23. typeof(Guid).GetMethod("Equals")
  24. ), new ParameterExpression[] { cs }
  25. );
  26.  
  27. return Connection.Model.GetTable<T>().Single(lambda);
  28. }
Add Comment
Please, Sign In to add comment