Advertisement
Guest User

EntityFrameWorkGetObjectByID

a guest
May 27th, 2011
2,895
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1. protected PropertyInfo GetProperty<TEntity>(TEntity entity) where TEntity : EntityObject
  2. {
  3.             return GetProperty<TEntity>(entity, entity.EntityKey.EntityKeyValues[0].Key);
  4. }
  5.  
  6. public bool PropertyEquals<TEntity>(TEntity entity, string value) where TEntity : EntityObject
  7. {
  8.     var prop = GetProperty<TEntity>(entity);
  9.     if (prop.GetValue(entity, null).ToString() == value)
  10.     {
  11.         return true;
  12.     }
  13.     else
  14.     {
  15.         return false;
  16.     }
  17. }
  18. public class TempRepo : RepositoryBase<myEntityFrameWorkObject>
  19. {
  20. }
  21.  
  22. public TEntity GetEntity<TEntity>(Func<TEntity, string, bool> whereClause, string id) where TEntity : EntityObject
  23. {
  24.     //Check to see if we have a null function, if so, dont run one
  25.     var runWhereClause = true;
  26.     if (whereClause == default(Func<TEntity, string, bool>))
  27.         runWhereClause = false;
  28.  
  29.     //Get all the types from the object context
  30.     //if the property type is the one we're looking for
  31.     //use a LINQ query to find it
  32.     var result = new List<TEntity>();
  33.     var newRepo = new TempRepo();
  34.     var properties = newRepo.UoW.ObjectContext.GetType().GetProperties().ToList();
  35.     foreach (PropertyInfo prop in properties)
  36.     {
  37.         if (prop.PropertyType.FullName.Contains(typeof(ObjectSet<TEntity>).FullName))
  38.         {
  39.             if (runWhereClause)
  40.                 result.AddRange((from i in
  41.                                      ((IEnumerable<TEntity>)prop.GetValue(newRepo.UoW.ObjectContext, null)).ToList()
  42.                                  where PropertyEquals(i, id)
  43.                                  select i).ToList());
  44.         }
  45.     }
  46.  
  47.     return result.First();
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement