/// <summary> /// Deletes all entities that match the WhereClause /// </summary> /// <typeparam name="T">Entity of type T</typeparam> /// <typeparam name="DT">Your DbContext type</typeparam> /// <param name="WhereClause">LINQ expression used to filter the data</param> public static void DeleteMultiple <T, DT>(Expression <Func <T, bool> > WhereClause) where DT : DbContext where T : class { DT db = DBContextFactory.GetDbContext <DT>(); DbSet <T> dbSet = db.Set <T>(); dbSet.RemoveRange(dbSet.Where(WhereClause)); }
/// <summary> /// Deletes all entities from the table of the specified type /// </summary> /// <typeparam name="T">Entity of type T</typeparam> /// <typeparam name="DT">Your DbContext type</typeparam> public static void DeleteAll <T, DT>() where DT : DbContext where T : class { DT db = DBContextFactory.GetDbContext <DT>(); DbSet <T> dbSet = db.Set <T>(); dbSet.RemoveRange(dbSet); db.SaveChanges(); }
/// <summary> /// Note that there are no async versions of some LINQ operators such as Where or OrderBy, /// because these only build up the LINQ expression tree and don't cause the query to be executed in the database. /// Only operators which cause query execution have async counterparts. /// </summary> /// <typeparam name="T">Type of entity you are querying</typeparam> /// <typeparam name="DT">Type of DbContext the entity is from</typeparam> /// <param name="Pager">A EFPager that simplifies paging through the entity table</param> /// <returns>A page of IQueryable sorted data of type T</returns> public static async Task <IQueryable <T> > QueryEntities <T, DT>(EFPager <T> Pager) where DT : DbContext where T : class { DT db = DBContextFactory.GetDbContext <DT>(); DbSet <T> dbSet = db.Set <T>(); IQueryable <T> filteredSet = dbSet; // (from x in dbSet select x); if (Pager.WhereClause != null) { filteredSet = filteredSet.Where(Pager.WhereClause); } IOrderedQueryable <T> ordered = null; // Apply all column sorting one at a time. We are simply modifying // the expression tree. No data is actually coming from the DB yet! foreach (ColumnSortInfo <T> sortColumn in Pager.SortOrder) { if (ordered == null) { if (sortColumn.SortAscending) { ordered = filteredSet.OrderBy(sortColumn.ColumnName); } else { ordered = filteredSet.OrderByDescending(sortColumn.ColumnName); } } else { if (sortColumn.SortAscending) { ordered = ordered.ThenBy(sortColumn.ColumnName); } else { ordered = ordered.ThenByDescending(sortColumn.ColumnName); } } } if (ordered != null) { filteredSet = ordered; } // Now that our query has been fully constructed, ask the database // how many records will be in the result. We can then calculate // all the necessary paging information. if (!Pager.Initialized) { int TotalRowCount = await filteredSet.CountAsync(); Pager.InitializePageInfo(TotalRowCount); } // Now ask for ONLY the page of data we are looking for filteredSet = filteredSet.Skip(Pager.Skip).Take(Pager.PageSize); // Update the page number so we can easily walk through a whole table Pager.MoveToNextPage(); return(filteredSet); }