mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 22:11:36 +08:00
Update demo
This commit is contained in:
parent
ebea347c58
commit
af2166ba90
@ -4,26 +4,58 @@ using System.Linq;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using SqlSugar;
|
using SqlSugar;
|
||||||
namespace OrmTest
|
|
||||||
|
namespace OrmTest
|
||||||
{
|
{
|
||||||
internal class _3_EasyQuery
|
internal class _3_EasyQuery
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 初始化方法,包含各种查询操作的演示
|
||||||
public static void Init()
|
/// Initialization method containing demonstrations of various query operations
|
||||||
|
/// </summary>
|
||||||
|
public static void Init()
|
||||||
{
|
{
|
||||||
|
// 创建表并插入一条记录
|
||||||
|
// Create table and insert a record
|
||||||
CreateTable();
|
CreateTable();
|
||||||
|
|
||||||
|
// 查询所有学生信息
|
||||||
|
// Query all student records
|
||||||
GetAllStudents();
|
GetAllStudents();
|
||||||
|
|
||||||
|
// 查询学生总数
|
||||||
|
// Get the total count of students
|
||||||
GetStudentCount();
|
GetStudentCount();
|
||||||
|
|
||||||
|
// 按条件查询学生信息
|
||||||
|
// Query student records based on conditions
|
||||||
GetStudentsByCondition();
|
GetStudentsByCondition();
|
||||||
|
|
||||||
|
// 模糊查询学生信息(名字包含"jack"的学生)
|
||||||
|
// Fuzzy search for student records (students with names containing "jack")
|
||||||
GetStudentsByName("jack");
|
GetStudentsByName("jack");
|
||||||
|
|
||||||
|
// 根据学生ID查询单个学生
|
||||||
|
// Query a single student by student ID
|
||||||
GetStudentById(1);
|
GetStudentById(1);
|
||||||
|
|
||||||
|
// 获取Student03表中的最大Id
|
||||||
|
// Get the maximum ID from the Student03 table
|
||||||
GetMaxStudentId();
|
GetMaxStudentId();
|
||||||
|
|
||||||
|
// 简单排序(按照Id降序排序)
|
||||||
|
// Simple sorting (sorting by Id in descending order)
|
||||||
GetStudentsOrderedByIdDesc();
|
GetStudentsOrderedByIdDesc();
|
||||||
|
|
||||||
|
// 查询学生姓名列表
|
||||||
|
// Query the list of student names
|
||||||
GetStudentNames();
|
GetStudentNames();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建表并插入一条记录
|
||||||
|
/// Create table and insert a record
|
||||||
|
/// </summary>
|
||||||
private static void CreateTable()
|
private static void CreateTable()
|
||||||
{
|
{
|
||||||
SqlSugarClient db = DbHelper.GetNewDb();
|
SqlSugarClient db = DbHelper.GetNewDb();
|
||||||
@ -32,32 +64,38 @@ namespace OrmTest
|
|||||||
.ExecuteCommand();
|
.ExecuteCommand();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查询所有学生信息
|
/// <summary>
|
||||||
// Query all student records
|
/// 查询所有学生信息
|
||||||
public static List<Student03> GetAllStudents()
|
/// Query all student records
|
||||||
|
/// </summary>
|
||||||
|
private static void GetAllStudents()
|
||||||
{
|
{
|
||||||
SqlSugarClient db = DbHelper.GetNewDb();
|
SqlSugarClient db = DbHelper.GetNewDb();
|
||||||
return db.Queryable<Student03>().ToList();
|
var students = db.Queryable<Student03>().ToList();
|
||||||
|
// 处理查询结果
|
||||||
|
// Process the query results
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查询学生总数
|
/// <summary>
|
||||||
// Get the total count of students
|
/// 查询学生总数
|
||||||
public static int GetStudentCount()
|
/// Get the total count of students
|
||||||
|
/// </summary>
|
||||||
|
private static void GetStudentCount()
|
||||||
{
|
{
|
||||||
SqlSugarClient db = DbHelper.GetNewDb();
|
SqlSugarClient db = DbHelper.GetNewDb();
|
||||||
return db.Queryable<Student03>().Count();
|
var count = db.Queryable<Student03>().Count();
|
||||||
|
// 处理查询结果
|
||||||
|
// Process the query results
|
||||||
}
|
}
|
||||||
|
|
||||||
// 按条件查询学生信息
|
/// <summary>
|
||||||
// Query student records based on conditions
|
/// 按条件查询学生信息
|
||||||
public static List<Student03> GetStudentsByCondition()
|
/// Query student records based on conditions
|
||||||
|
/// </summary>
|
||||||
|
private static void GetStudentsByCondition()
|
||||||
{
|
{
|
||||||
SqlSugarClient db = DbHelper.GetNewDb();
|
SqlSugarClient db = DbHelper.GetNewDb();
|
||||||
|
|
||||||
// 查询Id为1的学生
|
|
||||||
// Query students with Id equal to 1
|
|
||||||
var studentsWithId1 = db.Queryable<Student03>().Where(it => it.Id == 1).ToList();
|
|
||||||
|
|
||||||
// 查询name字段不为null的学生
|
// 查询name字段不为null的学生
|
||||||
// Query students where the 'name' field is not null
|
// Query students where the 'name' field is not null
|
||||||
var studentsWithNameNotNull = db.Queryable<Student03>().Where(it => it.Name != null).ToList();
|
var studentsWithNameNotNull = db.Queryable<Student03>().Where(it => it.Name != null).ToList();
|
||||||
@ -80,58 +118,74 @@ namespace OrmTest
|
|||||||
exp.OrIF(true, it => it.Id == 1);
|
exp.OrIF(true, it => it.Id == 1);
|
||||||
exp.Or(it => it.Name.Contains("jack"));
|
exp.Or(it => it.Name.Contains("jack"));
|
||||||
var studentsWithDynamicOr = db.Queryable<Student03>().Where(exp.ToExpression()).ToList();
|
var studentsWithDynamicOr = db.Queryable<Student03>().Where(exp.ToExpression()).ToList();
|
||||||
|
|
||||||
return studentsWithDynamicOr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 模糊查询
|
/// <summary>
|
||||||
// Fuzzy search
|
/// 模糊查询学生信息
|
||||||
public static List<Student03> GetStudentsByName(string keyword)
|
/// Fuzzy search for student records
|
||||||
|
/// </summary>
|
||||||
|
private static void GetStudentsByName(string keyword)
|
||||||
{
|
{
|
||||||
SqlSugarClient db = DbHelper.GetNewDb();
|
SqlSugarClient db = DbHelper.GetNewDb();
|
||||||
return db.Queryable<Student03>().Where(it => it.Name.Contains(keyword)).ToList();
|
var students = db.Queryable<Student03>().Where(it => it.Name.Contains(keyword)).ToList();
|
||||||
|
// 处理查询结果
|
||||||
|
// Process the query results
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据主键查询单个学生
|
/// <summary>
|
||||||
// Query a single student by primary key
|
/// 根据学生ID查询单个学生
|
||||||
public static Student03 GetStudentById(int id)
|
/// Query a single student by student ID
|
||||||
|
/// </summary>
|
||||||
|
private static void GetStudentById(int id)
|
||||||
{
|
{
|
||||||
SqlSugarClient db = DbHelper.GetNewDb();
|
SqlSugarClient db = DbHelper.GetNewDb();
|
||||||
return db.Queryable<Student03>().Single(it => it.Id == id);
|
var student = db.Queryable<Student03>().Single(it => it.Id == id);
|
||||||
|
// 处理查询结果
|
||||||
|
// Process the query results
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取订单表中的最大Id
|
/// <summary>
|
||||||
// Get the maximum Id from the Student03 table
|
/// 获取Student03表中的最大Id
|
||||||
public static int GetMaxStudentId()
|
/// Get the maximum ID from the Student03 table
|
||||||
|
/// </summary>
|
||||||
|
private static void GetMaxStudentId()
|
||||||
{
|
{
|
||||||
SqlSugarClient db = DbHelper.GetNewDb();
|
SqlSugarClient db = DbHelper.GetNewDb();
|
||||||
return db.Queryable<Student03>().Max(it => it.Id);
|
var maxId = db.Queryable<Student03>().Max(it => it.Id);
|
||||||
|
// 处理查询结果
|
||||||
|
// Process the query results
|
||||||
}
|
}
|
||||||
|
|
||||||
// 简单排序
|
/// <summary>
|
||||||
// Simple sorting
|
/// 简单排序(按照Id降序排序)
|
||||||
public static List<Student03> GetStudentsOrderedByIdDesc()
|
/// Simple sorting (sorting by Id in descending order)
|
||||||
|
/// </summary>
|
||||||
|
private static void GetStudentsOrderedByIdDesc()
|
||||||
{
|
{
|
||||||
SqlSugarClient db = DbHelper.GetNewDb();
|
SqlSugarClient db = DbHelper.GetNewDb();
|
||||||
return db.Queryable<Student03>().OrderBy(sc => sc.Id, OrderByType.Desc).ToList();
|
var students = db.Queryable<Student03>().OrderBy(sc => sc.Id, OrderByType.Desc).ToList();
|
||||||
|
// 处理查询结果
|
||||||
|
// Process the query results
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查询一列
|
/// <summary>
|
||||||
// Query a single column
|
/// 查询学生姓名列表
|
||||||
public static List<string> GetStudentNames()
|
/// Query the list of student names
|
||||||
|
/// </summary>
|
||||||
|
private static void GetStudentNames()
|
||||||
{
|
{
|
||||||
SqlSugarClient db = DbHelper.GetNewDb();
|
SqlSugarClient db = DbHelper.GetNewDb();
|
||||||
return db.Queryable<Student03>().Select(it => it.Name).ToList();
|
var studentNames = db.Queryable<Student03>().Select(it => it.Name).ToList();
|
||||||
|
// 处理查询结果
|
||||||
|
// Process the query results
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public class Student03
|
public class Student03
|
||||||
{
|
{
|
||||||
[SugarColumn(IsPrimaryKey =true,IsIdentity =true)]
|
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user