我们知道SQL里面有Count,计算有多少条记录,那么在EF中也有这样的函数封装:
//统计会员总数
//int total = context.account.Count(m=>m.Name.Contains("广"));
int total = context.account.Count();
判断是否含有某条记录:
//判断是否含有同名的会员
bool res = context.account.Any(m => m.Name == model.Name);
if (res)
{
return Content("已经存在该用户");
}
在EF中排序查找:
List<Account> list1 = context.account.Where(m => m.ID < 100).OrderByDescending(m=>m.ID).ToList();
获取Top条:
List<Account> list1 = context.account.Where(m => m.ID < 100).OrderByDescending(m=>m.ID).Take(3).ToList();
分页实现:
纯文本纯文本跳过多少条 取多少条
假如规定一页显示3条
第一页:跳过0 * 3条取3条
第二页:跳过1 * 3条取3条
第N页:跳过(N-1)*3条取3条
int pageSize = 3;//规定每页显示3条
List<Account> list1 = context.account.Where(m => m.ID < 100).OrderByDescending(m=>m.ID).Skip(((page??1)-1)*pageSize).Take(pageSize).ToList();
纯文本纯文本http://localhost:55751/home/index?page=1
http://localhost:55751/home/index?page=2
Previous: 08节-使用EF删除记录
Next: 10节-实体属性类型和表字段类型