| 
									
										
										
										
											2023-11-07 20:12:06 +08:00
										 |  |  |  | using OrmTest; | 
					
						
							|  |  |  |  | using SqlSugar; | 
					
						
							|  |  |  |  | using System; | 
					
						
							|  |  |  |  | using System.Linq; | 
					
						
							|  |  |  |  | using System.Threading.Tasks; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | public class _5_PageQuery | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     public static void Init() | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |      | 
					
						
							|  |  |  |  |         int pagenumber = 1; | 
					
						
							|  |  |  |  |         int pageSize = 20; | 
					
						
							|  |  |  |  |         int totalCount = 0; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         SqlSugarClient db = DbHelper.GetNewDb(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         //建表 | 
					
						
							|  |  |  |  |         //Create table | 
					
						
							|  |  |  |  |         AddTestData(db); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         // 同步分页方法 | 
					
						
							|  |  |  |  |         // Synchronous pagination method | 
					
						
							|  |  |  |  |         SyncPagination(db, pagenumber, pageSize, ref totalCount); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         // 异步分页方法 | 
					
						
							|  |  |  |  |         // Asynchronous pagination method | 
					
						
							|  |  |  |  |         AsyncPagination(db, pagenumber, pageSize) .GetAwaiter() .GetResult(); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     public static void AddTestData(SqlSugarClient db) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |      | 
					
						
							|  |  |  |  |         //建表 | 
					
						
							|  |  |  |  |         //Create table | 
					
						
							|  |  |  |  |         db.CodeFirst.InitTables<School, Student>(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         // 添加学校数据 | 
					
						
							|  |  |  |  |         // Add school data | 
					
						
							| 
									
										
										
										
											2023-11-07 20:49:14 +08:00
										 |  |  |  |         var school1 = new School { Id=1, Name = "School A" }; | 
					
						
							|  |  |  |  |         var school2 = new School {Id=2, Name = "School B" }; | 
					
						
							|  |  |  |  |         var id1=db.Insertable(school1).ExecuteReturnSnowflakeId(); | 
					
						
							|  |  |  |  |         var id2=db.Insertable(school2).ExecuteReturnSnowflakeId(); | 
					
						
							| 
									
										
										
										
											2023-11-07 20:12:06 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         // 添加学生数据 | 
					
						
							|  |  |  |  |         // Add student data | 
					
						
							| 
									
										
										
										
											2023-11-07 20:49:14 +08:00
										 |  |  |  |         var student1 = new Student { SchoolId =id1, Name = "John", CreateTime = DateTime.Now }; | 
					
						
							|  |  |  |  |         var student2 = new Student { SchoolId =id2, Name = "Alice", CreateTime = DateTime.Now };  | 
					
						
							| 
									
										
										
										
											2023-11-07 20:12:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-07 20:49:14 +08:00
										 |  |  |  |         db.Insertable(student1).ExecuteReturnSnowflakeId(); | 
					
						
							|  |  |  |  |         db.Insertable(student2).ExecuteReturnSnowflakeId(); | 
					
						
							| 
									
										
										
										
											2023-11-07 20:12:06 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         Console.WriteLine("Test data added successfully."); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// <summary> | 
					
						
							|  |  |  |  |     /// 同步分页示例 | 
					
						
							|  |  |  |  |     /// Synchronous pagination example | 
					
						
							|  |  |  |  |     /// </summary> | 
					
						
							|  |  |  |  |     /// <param name="db">数据库连接对象 Database connection object</param> | 
					
						
							|  |  |  |  |     /// <param name="pagenumber">页码 Page number</param> | 
					
						
							|  |  |  |  |     /// <param name="pageSize">每页大小 Page size</param> | 
					
						
							|  |  |  |  |     /// <param name="totalCount">总记录数 Total record count</param> | 
					
						
							|  |  |  |  |     public static void SyncPagination(SqlSugarClient db, int pagenumber, int pageSize, ref int totalCount) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         // 同步单表分页 | 
					
						
							|  |  |  |  |         // Synchronous pagination for a single table | 
					
						
							|  |  |  |  |         var page = db.Queryable<Student>().ToPageList(pagenumber, pageSize, ref totalCount); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         // 同步多表分页 | 
					
						
							|  |  |  |  |         // Synchronous pagination for multiple tables | 
					
						
							|  |  |  |  |         var list = db.Queryable<Student>().LeftJoin<School>((st, sc) => st.SchoolId == sc.Id) | 
					
						
							|  |  |  |  |                    .Select((st, sc) => new { Id = st.Id, Name = st.Name, SchoolName = sc.Name }) | 
					
						
							|  |  |  |  |                    .ToPageList(pagenumber, pageSize, ref totalCount); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         // offset分页 | 
					
						
							|  |  |  |  |         // offset pagination | 
					
						
							|  |  |  |  |         var sqlServerPage = db.Queryable<Student>().ToOffsetPage(pagenumber, pageSize); | 
					
						
							|  |  |  |  |            | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// <summary> | 
					
						
							|  |  |  |  |     /// 异步分页示例 | 
					
						
							|  |  |  |  |     /// Asynchronous pagination example | 
					
						
							|  |  |  |  |     /// </summary> | 
					
						
							|  |  |  |  |     /// <param name="db">数据库连接对象 Database connection object</param> | 
					
						
							|  |  |  |  |     /// <param name="pagenumber">页码 Page number</param> | 
					
						
							|  |  |  |  |     /// <param name="pageSize">每页大小 Page size</param> | 
					
						
							|  |  |  |  |     public static async Task AsyncPagination(SqlSugarClient db, int pagenumber, int pageSize) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         RefAsync<int> total = 0; | 
					
						
							|  |  |  |  |         // 异步分页 | 
					
						
							|  |  |  |  |         // Asynchronous pagination | 
					
						
							|  |  |  |  |         var orders = await db.Queryable<Student>().ToPageListAsync(pagenumber, pageSize, total); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  |     [SugarTable("Student05")] | 
					
						
							|  |  |  |  |     public class Student | 
					
						
							|  |  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-11-07 20:49:14 +08:00
										 |  |  |  |         [SugarColumn(IsPrimaryKey =true)] | 
					
						
							|  |  |  |  |         public long Id { get; set; } | 
					
						
							|  |  |  |  |         public long SchoolId { get; set; } | 
					
						
							| 
									
										
										
										
											2023-11-07 20:12:06 +08:00
										 |  |  |  |         public string Name { get; set; } | 
					
						
							|  |  |  |  |         public DateTime CreateTime { get; set; } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  |     [SugarTable("School05")] | 
					
						
							|  |  |  |  |     public class School | 
					
						
							|  |  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-11-07 20:49:14 +08:00
										 |  |  |  |         [SugarColumn( IsPrimaryKey = true)] | 
					
						
							|  |  |  |  |         public long Id { get; set; } | 
					
						
							| 
									
										
										
										
											2023-11-07 20:12:06 +08:00
										 |  |  |  |         public string Name { get; set; } | 
					
						
							|  |  |  |  |     }  | 
					
						
							|  |  |  |  | } |