查询

查询

作者:代富强 撰写日期:2020.8.17
查询:
根据上次一整天的模拟实战测试,从一开始拿到数据库文件的着急慌乱,和面对空白的面板不知从何下手,到测试结束后反复查看运用。对查询功能的运用算是有了一个大进步的认知。
在这里插入图片描述

首先连接两表开始查询:
public ActionResult SelectStudents(LayuiTablePage layuiTablePage, string studentNumber, string studentName, int? classID, string studentIDCard)
{
var listStudent = from tbStudent in myModel.S_Student
join tbClass in myModel.S_Class on tbStudent.classID equals tbClass.classID
select new StudentVo
{
studentID=tbStudent.studentID,//学生ID
classID=tbStudent.classID,//班级ID
studentNumber=tbStudent.studentNumber,//学生编号
studentName=tbStudent.studentName,//学生姓名
calssName=tbClass.calssName,//班级
studentSex=tbStudent.studentSex,//性别
telephone=tbStudent.telephone,//手机号
studentIDCard=tbStudent.studentIDCard,//身份证号
studentPicture=tbStudent.studentPicture,//图片
};
因为学生有些数据(比如说性别和姓名)可以相同而有些数据(比如说手机号和身份证号)是不能相同的,所以要对学生编号、姓名、班级ID、身份证号写条件筛选:
//条件筛选
//编号
if (!string.IsNullOrEmpty(studentNumber))
{
listStudent = listStudent.Where(m => m.studentNumber.Contains(studentNumber));
}
//姓名
if (!string.IsNullOrEmpty(studentName))
{
listStudent = listStudent.Where(m => m.studentName.Contains(studentName));
}
//班级ID
if (classID > 0)
{
listStudent = listStudent.Where(m => m.classID == classID);
}
//身份证号
if (!string.IsNullOrEmpty(studentIDCard))
{
listStudent = listStudent.Where(m => m.studentIDCard.Contains(studentIDCard));
}
紧接着要写学生数据的分页查询的部分,需要根据学生的ID进行倒叙排序还要跳过前面页数的数据并且查询本页数据的条数:
//分页查询学生数据
List listStu= listStudent
.OrderByDescending(m => m.studentID)//根据学生ID倒叙排序
.Skip(layuiTablePage.GetStartIndex())//跳过前面页数的数据
.Take(layuiTablePage.limit)//查询本页数据的条数
.ToList();//返回List集合
//查询学生数据的总条数
int intTotalRow = myModel.S_Student.Count();
//准备Layui Table所需的数据格式(泛型)
LayuiTableData layuiTableData = new LayuiTableData()
{
count = intTotalRow,//总条数
data = listStu,//本页的数据
};
//返回Json
return Json(layuiTableData, JsonRequestBehavior.AllowGet);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值