EF (Entity Framework) General purpose DBHelper General class, additions and deletions and lists

Source: Internet
Author: User
Tags rowcount

(1) General class Usage

which

Generic class Name: DBHelper

Entity class: UserInfo

1   //New2Dbhelper<userinfo> DBHelper =NewDbhelper<userinfo>();3UserInfo UserInfo =NewUserInfo ();4UserInfo. Name ="1";5 DBHelper. ADD (userinfo);6 7   //find a list by criteria8   varEntityes = DBHelper. Findlist (x = X.name = ="1");9list<userinfo> list = Entityes. Tolist<userinfo>();Ten  OneResponse.Write ("the number of names is 1:"+list. Count); A  -   //Pagination Lookup -   intRowCount =0; the   varEntityes2 = DBHelper. Findpagedlist (2,Ten, outrowcount,x=>x.name=="1", S=>s.id,true);

X EF (Entity Framework) General purpose DBHelper General class, adding and removing changes and lists

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Linq.Expressions;usingsystem.web; Public classDbhelper<t>whereT:class{aprosysentities Context=Newaprosysentities (); /// <summary>    ///add a new entity/// </summary>    /// <param name= "entity" ></param>    /// <returns></returns>     Public intAdd (T entity) {context. Entry<T> (entity). State =System.Data.EntityState.Added; returncontext.    SaveChanges (); }    /// <summary>    ///Delete an entity/// </summary>    /// <param name= "entity" ></param>    /// <returns></returns>     Public intRemove (T entity) {context. Entry<T> (entity). State =System.Data.EntityState.Deleted; returncontext.    SaveChanges (); }    /// <summary>    ///Modify an entity/// </summary>    /// <param name= "entity" ></param>    /// <returns></returns>     Public intUpdate (T entity) {context. Entry<T> (entity). State =System.Data.EntityState.Modified; returncontext.    SaveChanges (); }    /// <summary>    ///Bulk New Entities/// </summary>    /// <param name= "DbContext" ></param>    /// <returns></returns>     Public intAddlist (paramst[] entities) {        intresult =0;  for(inti =0; I < entities. Count (); i++)        {            if(Entities[i] = =NULL)                Continue; Context. Entry<T> (Entities[i]). State =System.Data.EntityState.Added; if(I! =0&& I% -==0) {result+=context.            SaveChanges (); }        }        if(Entities. Count () >0) Result+=context.        SaveChanges (); returnresult; }    /// <summary>    ///Bulk Delete Entities/// </summary>    /// <param name= "where" ></param>    /// <returns></returns>     Public intRemovelist (Expression<func<t,BOOL>>where)    {        varTemp = context. Set<t> (). Where (where); foreach(varIteminchtemp) {context. Entry<T> (item). State =System.Data.EntityState.Deleted; }        returncontext.    SaveChanges (); }    /// <summary>    ///Query by Criteria/// </summary>    /// <param name= "where" ></param>    /// <returns></returns>     PublicIqueryable<t> Findlist (Expression<func<t,BOOL>>where)    {        varTemp = context. Set<t> (). Where (where); returntemp; }    /// <summary>    ///query by criteria, sort/// </summary>    /// <typeparam name= "S" ><peparam>    /// <param name= "where" ></param>    /// <param name= "by" ></param>    /// <param name= "ISASC" ></param>    /// <returns></returns>     PublicIqueryable<t> findlist<s> (Expression<func<t,BOOL>>where, Expression<func<t, S>>,BOOLISASC) {        varList = context. Set<t> (). Where (where); if(ISASC) List= list. Orderby<t, s>(a); ElseList= list. Orderbydescending<t, s>(a); returnlist; }    /// <summary>    ///query by criteria, paging/// </summary>    /// <param name= "PageIndex" ></param>    /// <param name= "pageSize" ></param>    /// <param name= "RowCount" ></param>    /// <param name= "where" ></param>    /// <returns></returns>     PublicIqueryable<t> Findpagedlist (intPageIndex,intPageSize, out intRowCount, Expression<func<t,BOOL>>where)    {        varList = context. Set<t> (). Where (where); RowCount=list.        Count (); List= list. Skip (PageSize * (PageIndex-1)).        Take (pageSize); returnlist; }    /// <summary>    ///query by criteria, paging, sorting/// </summary>    /// <typeparam name= "S" ><peparam>    /// <param name= "PageIndex" ></param>    /// <param name= "pageSize" ></param>    /// <param name= "RowCount" ></param>    /// <param name= "where" ></param>    /// <param name= "by" ></param>    /// <param name= "ISASC" ></param>    /// <returns></returns>     PublicIqueryable<t> findpagedlist<s> (intPageIndex,intPageSize, out intRowCount, Expression<func<t,BOOL>>where, Expression<func<t, S>>,BOOLISASC) {        varList = context. Set<t> (). Where (where); RowCount=list.        Count (); if(ISASC) List= list. Orderby<t, s>. Skip (PageSize * (PageIndex-1)).        Take (pageSize); ElseList= list. Orderbydescending<t, s>. Skip (PageSize * (PageIndex-1)).        Take (pageSize); returnlist; }}

EF (Entity Framework) General purpose DBHelper General class, additions and deletions and lists

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.