Using EF and C # generics to implement Universal paging queries

Source: Internet
Author: User

using EF and C # generics to implement universal paging queriesThe Entity Framework is Microsoft's ORM framework, an object-relational correspondence (ORM) solution developed on the basis of ADO. This framework associated the table information in the database with the entity class object through XML, so that developers only need to care about the entity object, without needing to manipulate the database manually, the modification of the entity object will be mapped to the database, which greatly improves the development efficiency. The following code uses the EF, generics, generics delegate, Lambda, anonymous class, dynamic type, and other knowledge to complete the crud of EF, and provides an efficient universal paging query method that takes a deferred load and the return value type is IQueryable.
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Data.Entity;4 usingSystem.Linq;5 usingSystem.Linq.Expressions;6 usingSystem.Text;7 8 namespaceeftest9 {Ten     class Program One     { A         Static voidMain (string[] args) -         { -Testentities context =Newtestentities (); the  -             //Increase -             //t_test T = new T_test () {name= "Kobayashi"};  -             //context. T_test.add (t); +             //context. SaveChanges (); -  +  A             //Delete at             //Lambda mode -             //var t = context. T_test.where (u=> (u.id==1)). FirstOrDefault (); -  -             //LINQ to SQL mode -             //var t = (from u in context. T_test -             //where u.id = = 1 in             //select u). FirstOrDefault (); -             //context. T_test.remove (t); to             //context. SaveChanges (); +             //the deletion of data is more convenient and more efficient in this way -             //t_test T = new T_test () {id=2}; the             //context. Entry<t_test> (T). state=entitystate.deleted; *             //context. SaveChanges (); $ Panax Notoginseng             //Change -             //var t = (from u in context. T_test the             //where u.id = = 2 +             //select u). FirstOrDefault (); A             //t.name = "Xiao Lin"; the             //context. SaveChanges (); +  -             //Check $             //var t = (from u in context. T_test $             //where u.id = = 2 -             //select u). FirstOrDefault (); -             //Console.WriteLine (t.id + t.name); the              -             //General Page PagingWuyi             intcount;  theGetpagelistparameter<t_test,int> parameter=NewGetpagelistparameter<t_test,int>(); -Parameter.isasc=true; WuParameter.orderbylambda = s = =s.id; -Parameter.pageindex =1; AboutParameter.pagesize =3; $Parameter.selectlambda = s = =NewPerson{name=S.name}; -Parameter.wherelambda = s = = s.id >3; -  -             vardata = Getpagelist<t_test,int> (parameter, outcount); A  +             foreach(Person tinchdata) the             { - Console.WriteLine (t.name); $             } the  the Console.readkey (); the         } the  -          Public StaticList<dynamic> getpagelist<t, tkey> (getpagelistparameter<t, tkey> parameter, out intCountwhereT:class in         { theTestentities context =Newtestentities (); the             //Note Order AboutCount = context. Set<t> (). Where<t>(PARAMETER.WHERELAMBDA). Count (); the             varList = context. Set<t> (). Where<t>(PARAMETER.WHERELAMBDA); the             if(PARAMETER.ISASC) the             { +List =list. (PARAMETER.ORDERBYLAMBDA); -             } the             ElseBayi             { theList =list. OrderByDescending (PARAMETER.ORDERBYLAMBDA); the             } -  -             returnList. Skip ((Parameter.pageindex-1) *parameter.pagesize). Take (parameter.pagesize). Select (PARAMETER.SELECTLAMBDA). ToList (); the         } the     } the  the     //take the required columns -      Public class Person the     { the          Public stringName {Get;Set; } the     }94  the     //Encapsulation Method Parameters the      Public classGetpagelistparameter<t, tkey> the     {98          PublicExpression<func<t, dynamic>> Selectlambda {Get;Set; } About          PublicExpression<func<t,BOOL>> Wherelambda {Get;Set; } -          PublicExpression<func<t, tkey>> Orderbylambda {Get;Set; }101          Public intpageSize {Get;Set; }102          Public intPageIndex {Get;Set; }103          Public BOOLISASC {Get;Set; }104     } the}

Using EF and C # generics to implement Universal paging queries

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.