Beispiel #1
0
 public List <T> Filter(Expression <Func <T, bool> > predicate)
 {
     using (ModelContainer1 context = new ModelContainer1())
     {
         return((List <T>)context.Set <T>().Where(predicate).ToList());
     }
 }
Beispiel #2
0
 public T Single(Expression <Func <T, bool> > predicate)
 {
     using (ModelContainer1 context = new ModelContainer1())
     {
         return(context.Set <T>().FirstOrDefault(predicate));
     }
 }
Beispiel #3
0
 public List <T> GetAll()
 {
     using (ModelContainer1 context = new ModelContainer1())
     {
         return((List <T>)context.Set <T>().ToList());
     }
 }
Beispiel #4
0
 public bool Create(T entity)
 {
     using (ModelContainer1 context = new ModelContainer1())
     {
         context.Set <T>().Add(entity);
         return(context.SaveChanges() > 0);
     }
 }
Beispiel #5
0
 public void Delete(Expression <Func <T, bool> > predicate)
 {
     using (ModelContainer1 context = new ModelContainer1())
     {
         var entities = context.Set <T>().Where(predicate).ToList();
         entities.ForEach(x => context.Entry(x).State = EntityState.Deleted);
         context.SaveChanges();
     }
 }
Beispiel #6
0
        public List <T> Filter(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 (ModelContainer1 context = new ModelContainer1())
            {
                DbQuery <T> query = context.Set <T>();

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

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