利用EF和C#泛型實現通用分頁查詢

來源:互聯網
上載者:User

標籤:des   style   blog   io   ar   color   os   使用   sp   

利用EF和C#泛型實現通用分頁查詢      Entity Framework 是微軟以 ADO.NET 為基礎所發展出來的對象關係對應 (ORM) 解決方案,是微軟的ORM架構。此架構將資料庫中的表資訊通過xml與實體類對象相關聯,使得開發人員只需要關心實體物件,而不需要手動操作資料庫,對實體物件的修改會映射到資料庫中,這樣大大提高了開發效率。以下代碼使用了EF、泛型、泛型委派、lambda、匿名類、dynamic動態類型等知識完成了EF的crud,並且提供了一個高效的通用分頁查詢方法,採用了延時載入,傳回值類型是IQueryable。
  1 using System;  2 using System.Collections.Generic;  3 using System.Data.Entity;  4 using System.Linq;  5 using System.Linq.Expressions;  6 using System.Text;  7   8 namespace EFtest  9 { 10     class Program 11     { 12         static void Main(string[] args) 13         { 14             testEntities context = new testEntities(); 15  16             //增 17             //T_Test t = new T_Test() {Name="小林" };           18             //context.T_Test.Add(t); 19             //context.SaveChanges(); 20  21  22             //刪 23             //lambda方式 24             //var t = context.T_Test.Where(u=>(u.Id==1)).FirstOrDefault(); 25  26             //linq to sql方式 27             //var t = (from u in context.T_Test 28             //         where u.Id == 1 29             //         select u).FirstOrDefault(); 30             //context.T_Test.Remove(t); 31             //context.SaveChanges(); 32             //資料的刪除用這種方式更方便,效率更高 33             //T_Test t = new T_Test() { Id=2 }; 34             //context.Entry<T_Test>(t).State=EntityState.Deleted; 35             //context.SaveChanges(); 36  37             //改 38             //var t = (from u in context.T_Test 39             //         where u.Id == 2 40             //         select u).FirstOrDefault(); 41             //t.Name = "小林"; 42             //context.SaveChanges(); 43  44             //查 45             //var t = (from u in context.T_Test 46             //         where u.Id == 2 47             //         select u).FirstOrDefault(); 48             //Console.WriteLine(t.Id + t.Name); 49              50             //通用分頁 51             int count;           52             GetPageListParameter<T_Test, int> parameter=new GetPageListParameter<T_Test,int>(); 53             parameter.isAsc=true; 54             parameter.orderByLambda = s => s.Id; 55             parameter.pageIndex = 1; 56             parameter.pageSize = 3; 57             parameter.selectLambda = s => new Person{Name=s.Name }; 58             parameter.whereLambda = s => s.Id > 3; 59  60             var data = GetPageList<T_Test, int>(parameter, out count); 61  62             foreach (Person t in data) 63             { 64                 Console.WriteLine(t.Name); 65             } 66  67             Console.ReadKey(); 68         } 69  70         public static List<dynamic> GetPageList<T, TKey>(GetPageListParameter<T, TKey> parameter, out int count) where T : class 71         { 72             testEntities context = new testEntities(); 73             //注意順序 74             count = context.Set<T>().Where<T>(parameter.whereLambda).Count(); 75             var list = context.Set<T>().Where<T>(parameter.whereLambda); 76             if (parameter.isAsc) 77             { 78                 list = list.OrderBy(parameter.orderByLambda); 79             } 80             else 81             { 82                 list = list.OrderByDescending(parameter.orderByLambda); 83             } 84  85             return list.Skip((parameter.pageIndex - 1) * parameter.pageSize).Take(parameter.pageSize).Select(parameter.selectLambda).ToList(); 86         } 87     } 88  89     //取所需列 90     public class Person 91     { 92         public string Name { get; set; } 93     } 94  95     //封裝方法參數 96     public class GetPageListParameter<T, TKey> 97     { 98         public Expression<Func<T, dynamic>> selectLambda { get; set; } 99         public Expression<Func<T, bool>> whereLambda { get; set; }100         public Expression<Func<T, TKey>> orderByLambda { get; set; }101         public int pageSize { get; set; }102         public int pageIndex { get; set; }103         public bool isAsc { get; set; }104     }105 }

 

利用EF和C#泛型實現通用分頁查詢

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.