Example #1
0
 public List <T> Filtrar(Expression <Func <T, bool> > predicate)
 {
     using (EContext context = new EContext())
     {
         return((List <T>)context.Set <T>().Where(predicate).ToList());
     }
 }
Example #2
0
 public T Obtener(Expression <Func <T, bool> > predicate)
 {
     using (EContext context = new EContext())
     {
         return(context.Set <T>().FirstOrDefault(predicate));
     }
 }
Example #3
0
 public List <T> Listar()
 {
     using (EContext context = new EContext())
     {
         return((List <T>)context.Set <T>().ToList());
     }
 }
Example #4
0
        public bool Eliminar(Expression <Func <T, bool> > predicate)
        {
            bool blResultado = false;

            using (EContext context = new EContext())
            {
                try
                {
                    var entities = context.Set <T>().Where(predicate).ToList();
                    entities.ForEach(x => context.Entry(x).State = EntityState.Deleted);
                    context.SaveChanges();
                    blResultado = true;
                }
                catch (Exception ex)
                {
                    blResultado = false;
                    throw new Exception(ex.Message);
                }
            }
            return(blResultado);
        }
Example #5
0
        public bool Crear(T entity)
        {
            bool blResultado = false;

            using (EContext context = new EContext())
            {
                try
                {
                    context.Set <T>().Add(entity);
                    context.SaveChanges();

                    blResultado = true;
                }
                catch (Exception ex)
                {
                    blResultado = false;
                    throw new Exception(ex.Message);
                }
            }
            return(blResultado);
        }
Example #6
0
        public List <T> Filtrar(Expression <Func <T, bool> > predicate, List <Expression <Func <T, object> > > includes)
        {
            List <string> includelist = new List <string>();

            foreach (var item in includes)
            {
                MemberExpression body = item.Body as MemberExpression;
                if (body == null)
                {
                    throw new ArgumentException("The body must be a member expression");
                }

                includelist.Add(body.Member.Name);
            }

            using (EContext context = new EContext())
            {
                DbQuery <T> query = context.Set <T>();

                includelist.ForEach(x => query = query.Include(x));

                return((List <T>)query.Where(predicate).ToList());
            }
        }