首页 > 代码库 > linq 条件查询与分页
linq 条件查询与分页
<div>姓名:<asp:TextBox ID="T1" runat="server"></asp:TextBox></div> <div> 性别:<asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem Text="男和女" Value=http://www.mamicode.com/"Null"></asp:ListItem> <asp:ListItem Text="男" Value=http://www.mamicode.com/"True"></asp:ListItem> <asp:ListItem Text="女" Value=http://www.mamicode.com/"False"></asp:ListItem> </asp:DropDownList> </div> <div> 成绩:<asp:DropDownList ID="DropDownList2" runat="server"> <asp:ListItem Text="不限" Value=http://www.mamicode.com/"Null"></asp:ListItem> <asp:ListItem Text="大于" Value=http://www.mamicode.com/">"></asp:ListItem> <asp:ListItem Text="小于" Value=http://www.mamicode.com/"<"></asp:ListItem> </asp:DropDownList><asp:TextBox ID="T2" runat="server"></asp:TextBox> </div> <asp:Button ID="Button2" runat="server" Text="查询" /><br /> 当前页数:<asp:Label ID="L2" runat="server" Text="1"></asp:Label> 总页数:<asp:Label ID="L3" runat="server" Text="1"></asp:Label><br /> <asp:Button ID="Button3" runat="server" Text="上一页" /><asp:Button ID="Button6" runat="server" Text="下一页" />
int Pcount = 2; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) {//绑定数据,跳过0条,取2条 using (StudentsDataClassesDataContext con = new StudentsDataClassesDataContext()) { Repeater1.DataSource = SS(con).Take(Pcount); Repeater1.DataBind(); L3.Text = MaxP().ToString(); } //Repeater1.DataSource = con.Stu.ToList(); //Repeater1.DataBind(); } Button1.Click += Button1_Click; Button2.Click += Button2_Click; //Button4.Click += Button4_Click; //Button5.Click += Button5_Click; Button3.Click += Button3_Click; Button6.Click += Button6_Click; } void Button6_Click(object sender, EventArgs e) { using (StudentsDataClassesDataContext con = new StudentsDataClassesDataContext()) { int a = Convert.ToInt32(L2.Text) + 1; if (a > Convert.ToInt32(MaxP())) { return; } Repeater1.DataSource = SS(con).Skip((a - 1) * Pcount).Take(Pcount); Repeater1.DataBind(); L2.Text = a.ToString(); } } void Button3_Click(object sender, EventArgs e) { using (StudentsDataClassesDataContext con = new StudentsDataClassesDataContext()) { int a = Convert.ToInt32(L2.Text) - 1; if (a < 1) { return; } Repeater1.DataSource = SS(con).Skip((a - 1) * Pcount).Take(Pcount); Repeater1.DataBind(); L2.Text = a.ToString(); } } void Button2_Click(object sender, EventArgs e) { using (StudentsDataClassesDataContext con = new StudentsDataClassesDataContext()) { Repeater1.DataSource = SS(con).Take(Pcount); Repeater1.DataBind(); L2.Text = "1"; L3.Text = MaxP().ToString(); } } //void Button5_Click(object sender, EventArgs e) //{ // Response.Redirect("toupiao.aspx"); //} //void Button4_Click(object sender, EventArgs e) //{ // Response.Cookies["id2"].Expires = DateTime.Now.AddDays(-10); //} //添加 void Button1_Click(object sender, EventArgs e) { Response.Redirect("Add.aspx"); } //把组合查询封装成一个方法 public List<Stu> SS(StudentsDataClassesDataContext con) { List<Stu> s = con.Stu.ToList(); if (T1.Text.Trim().Length > 0) { s = s.Where(r => r.Name.Contains(T1.Text.Trim())).ToList(); } if (DropDownList1.SelectedValue != "Null") { s = s.Where(r => r.Sex == Convert.ToBoolean(DropDownList1.SelectedValue)).ToList(); } if (DropDownList2.SelectedValue != "Null") { if (DropDownList2.SelectedValue =http://www.mamicode.com/= ">") { s = s.Where(r => r.Score > Convert.ToInt32((T2.Text.Trim()))).ToList(); } else { s = s.Where(r => r.Score < Convert.ToInt32((T2.Text.Trim()))).ToList(); } } return s; } ////获取最大页数 public int MaxP() { using (StudentsDataClassesDataContext con = new StudentsDataClassesDataContext()) { return Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(SS(con).Count) / Pcount)); } }
linq 条件查询与分页
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。