LinQ的组合+分页
前台代码:
名称:<asp:TextBox ID="Textname" runat="server"></asp:TextBox> 油耗:<asp:TextBox ID="Textoil" runat="server"></asp:TextBox> 马力:<asp:TextBox ID="Textpowers" runat="server"></asp:TextBox> <asp:Button ID="btncha" runat="server" Text="查询" /><br /><br />
当前第[<asp:Label ID="lb_now" runat="server" Text="0"></asp:Label>]页,共[<asp:Label ID="lb_all" runat="server" Text="0"></asp:Label>]页 <asp:LinkButton ID="btnfirst" runat="server">首页</asp:LinkButton> <asp:LinkButton ID="btn_prev" runat="server">上一页</asp:LinkButton> <asp:LinkButton ID="btn_next" runat="server">下一页</asp:LinkButton> <asp:LinkButton ID="btnlast" runat="server">末页</asp:LinkButton> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"></asp:DropDownList>
CarDA:数据访问类,
mydbDataContext con = new mydbDataContext();
public List<Car> Select()//查询表所有数据
{
return con.Car.ToList();
}
public List<Car> Selectname(string n)//查名称的方法 { return con.Car.Where(r => r.Name.Contains(n)).ToList(); } public List<Car> Selectoil(decimal o)//查油耗的方法 { return con.Car.Where(r => r.Oil == o).ToList(); } public List<Car> Selectpow(int p)//查马力的方法 { return con.Car.Where(r => r.Powers == p).ToList(); } public List<Car> Fenye(List<Car> list, int s, int t)//查分页的方法s是几条数据,t是几页 { List<Car> li = con.Car.ToList();//查到所有数据 return li.Intersect(list).Skip(s * t).Take(t).ToList(); }
后台代码:
int pagenum = 3;//每页显示三页 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Repeater1.DataSource = new CarDA().Select().Take(pagenum); Repeater1.DataBind(); lb_now.Text = "1";//定义打开显示第一页 lb_all.Text = Math.Ceiling(Convert.ToDouble(new CarDA().Select().Count) / pagenum).ToString();//上限 最大页数 总条数÷一页显示的条数= 总共的页数 for (int i = 1; i <= Convert.ToInt32(lb_all.Text); i++) { ListItem li = new ListItem(i.ToString(), i.ToString()); DropDownList1.Items.Add(li);//跳转 } }
btncha.Click += btncha_Click;//查询 btn_prev.Click += btn_prev_Click;//上一页 btn_next.Click += btn_next_Click;//下一页 btnfirst.Click += btnfirst_Click;//首页 btnlast.Click += btnlast_Click;//末页 DropDownList1.SelectedIndexChanged += DropDownList1_SelectedIndexChanged;//跳转 } void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) {//跳转 int n = Convert.ToInt32(DropDownList1.SelectedItem.Text);//获取索引最小的选定向 lb_now.Text = n.ToString();//当前页数 CarDA ca = new CarDA(); List<Car> list = ca.Select(); if (Textname.Text != "") { List<Car> list1 = ca.Selectname(Textname.Text); list = list.Intersect(list1).ToList(); } if (Textoil.Text != "") { List<Car> list1 = ca.Selectoil(Convert.ToDecimal(Textoil.Text)); list = list.Intersect(list1).ToList(); } if (Textpowers.Text != "") { List<Car> list1 = ca.Selectpow(Convert.ToInt32(Textpowers.Text)); list = list.Intersect(list1).ToList(); } lb_all.Text = Math.Ceiling(Convert.ToDouble(list.Count) / pagenum).ToString(); Repeater1.DataSource = ca.Fenye(list, (Convert.ToInt32(lb_now.Text) - 1), pagenum); Repeater1.DataBind(); } void btnlast_Click(object sender, EventArgs e) {//末页 CarDA ca = new CarDA(); List<Car> list = ca.Select(); if (Textname.Text != "") { List<Car> list1 = ca.Selectname(Textname.Text); list = list.Intersect(list1).ToList(); } if (Textoil.Text != "") { List<Car> list1 = ca.Selectoil(Convert.ToDecimal(Textoil.Text)); list = list.Intersect(list1).ToList(); } if (Textpowers.Text != "") { List<Car> list1 = ca.Selectpow(Convert.ToInt32(Textpowers.Text)); list = list.Intersect(list1).ToList(); } lb_now.Text = Math.Ceiling(Convert.ToDouble(list.Count) / pagenum).ToString(); lb_all.Text = Math.Ceiling(Convert.ToDouble(list.Count) / pagenum).ToString(); Repeater1.DataSource = ca.Fenye(list, (Convert.ToInt32(lb_now.Text) - 1), pagenum); Repeater1.DataBind(); } void btnfirst_Click(object sender, EventArgs e) {//首页 lb_now.Text = "1"; CarDA ca = new CarDA(); List<Car> list = ca.Select(); if (Textname.Text != "") { List<Car> list1 = ca.Selectname(Textname.Text); list = list.Intersect(list1).ToList(); } if (Textoil.Text != "") { List<Car> list1 = ca.Selectoil(Convert.ToDecimal(Textoil.Text)); list = list.Intersect(list1).ToList(); } if (Textpowers.Text != "") { List<Car> list1 = ca.Selectpow(Convert.ToInt32(Textpowers.Text)); list = list.Intersect(list1).ToList(); } lb_all.Text = Math.Ceiling(Convert.ToDouble(list.Count) / pagenum).ToString(); Repeater1.DataSource = ca.Fenye(list, (Convert.ToInt32(lb_now.Text) - 1), pagenum); Repeater1.DataBind(); } void btn_next_Click(object sender, EventArgs e) {//下一页 int a =Convert.ToInt32( lb_now.Text)+1;每点一下加1 CarDA ca = new CarDA(); List<Car> list = ca.Select(); if (Textname.Text != "") { List<Car> list1 = ca.Selectname(Textname.Text); list = list.Intersect(list1).ToList(); } if (Textoil.Text != "") { List<Car> list1 = ca.Selectoil(Convert.ToDecimal(Textoil.Text)); list = list.Intersect(list1).ToList(); } if (Textpowers.Text != "") { List<Car> list1 = ca.Selectpow(Convert.ToInt32(Textpowers.Text)); list = list.Intersect(list1).ToList(); } lb_now.Text = a.ToString();//让现在的页数显示+1之后的数据 lb_all.Text = Math.Ceiling(Convert.ToDouble(list.Count) / pagenum).ToString(); Repeater1.DataSource = ca.Fenye(list, (Convert.ToInt32(lb_now.Text) - 1), pagenum); Repeater1.DataBind(); } void btn_prev_Click(object sender, EventArgs e) {//上一页 if (Convert.ToInt32(lb_now.Text) > 1)判断是否可以点上一页 { int nownum = Convert.ToInt32(lb_now.Text) - 1; CarDA ca = new CarDA(); List<Car> list = ca.Select(); if (Textname.Text != "") { List<Car> list1 = ca.Selectname(Textname.Text); list = list.Intersect(list1).ToList(); } if (Textoil.Text != "") { List<Car> list1 = ca.Selectoil(Convert.ToDecimal(Textoil.Text)); list = list.Intersect(list1).ToList(); } if (Textpowers.Text != "") { List<Car> list1 = ca.Selectpow(Convert.ToInt32(Textpowers.Text)); list = list.Intersect(list1).ToList(); } Repeater1.DataSource = ca.Fenye(list, (nownum - 1), pagenum); Repeater1.DataBind(); lb_now.Text = nownum.ToString(); } } void btncha_Click(object sender, EventArgs e) {//查询 lb_now.Text = "1"; int nownum = Convert.ToInt32(lb_now.Text); CarDA ca = new CarDA(); List<Car> list = ca.Select(); if (Textname.Text != "") { List<Car> list1 = ca.Selectname(Textname.Text); list = list.Intersect(list1).ToList(); } if (Textoil.Text != "") { List<Car> list1 = ca.Selectoil(Convert.ToDecimal(Textoil.Text)); list = list.Intersect(list1).ToList(); } if (Textpowers.Text != "") { List<Car> list1 = ca.Selectpow(Convert.ToInt32(Textpowers.Text)); list = list.Intersect(list1).ToList(); } lb_all.Text = Math.Ceiling(Convert.ToDouble(list.Count) / pagenum).ToString(); DropDownList1.Items.Clear(); for (int i = 1; i <= Convert.ToInt32(lb_all.Text); i++) { ListItem li = new ListItem(i.ToString(), i.ToString()); DropDownList1.Items.Add(li); } Repeater1.DataSource = ca.Fenye(list, (nownum - 1), pagenum); Repeater1.DataBind(); }