EF

ShopDB db = new ShopDB();

//分页显示商品
[HttpGet]
public PageDate GetGoods(int index, int size)
{
PageDate page = new PageDate();
var list = db.Goods.ToList();
var count = list.Count();
page.List = list.OrderBy(x => x.GId).Skip((index - 1) * size).Take(size).ToList();
page.PageCount = count / size + (count % size == 0 ? 0 : 1);
return page;
}
//分页存储过程显示
[HttpGet]
public PageDate GetGoods2(int index, int size)
{
//实例化参数
SqlParameter[] parameters = new SqlParameter[]
{
new SqlParameter("@index",index),
new SqlParameter("@size",size),
new SqlParameter("@totalcount",SqlDbType.Int), //总数据数
new SqlParameter("@pagecount",SqlDbType.Int), //总页数
};
//指定输出参数
parameters[2].Direction = ParameterDirection.Output;
parameters[3].Direction = ParameterDirection.Output;

//存储过程查询
var list = db.Database.SqlQuery<Goods>("exec sp_Show @index,@size,@totalcount out,@pagecount out", parameters).ToList();

PageDate page = new PageDate();
page.List = list;
page.PageCount = int.Parse(parameters[3].Value.ToString());
return page;
}

//商品表
public class Goods
{
[Key]
public int GId { get; set; }
public string Name { get; set; }
public string Img { get; set; }
public decimal Price { get; set; }
}
//购物车表
public class ShopCar
{
[Key]
public int SId { get; set; }
public int BuyCount { get; set; }

public Goods Goods { get; set; }
public int GoodsGId { get; set; }
}
//订单表
public class Order
{
[Key]
public int OId { get; set; }
public string OrderNum { get; set; }
public DateTime CreateTime { get; set; }

public Goods Goods { get; set; }
public int GoodsGId { get; set; }
}

public class PageDate
{
public List<Goods> List { get; set; }
public int PageCount { get; set; }
}

 
原文地址:https://www.cnblogs.com/CoreColor/p/13448855.html