linq 条件查询与分页

<div>姓名:<asp:TextBox ID="T1" runat="server"></asp:TextBox></div>
        <div>
            性别:<asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem Text="男和女" Value="Null"></asp:ListItem>
                <asp:ListItem Text="男" Value="True"></asp:ListItem>
                <asp:ListItem Text="女" Value="False"></asp:ListItem>
            </asp:DropDownList>
        </div>
        <div>
            成绩:<asp:DropDownList ID="DropDownList2" runat="server">
                <asp:ListItem Text="不限" Value="Null"></asp:ListItem>
                <asp:ListItem Text="大于" Value=">"></asp:ListItem>
                <asp:ListItem Text="小于" Value="<"></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 == ">")
            { 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));
        }
    }
时间: 2024-10-12 08:48:53

linq 条件查询与分页的相关文章

Linq 组合查询与分页查询

后台: using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class ZS : System.Web.UI.Page { Hashtable ht = new Hashtable(); in

Linq高级查询与分页查询

Linq高级查询 以~开头: r=>r.Name.StartsWith("李"); 以~结尾: r=>r.Name.EndsWith("光"); 包含(模糊查询): r=>r.Name.Contains("四"); 数据总个数: Con.Goods.Count();||Con.Users.ToList().count; 最大值: Con.Goods.ToList().Max(r=>r.Price); 最小值: Con.Go

(四)Oracle条件查询,分页查询

1. SQL(基础查询) 1.1基础查询 1.1.1. 使用LIKE条件(模糊查询) 当用户在执行查询时,不能完全确定某些信息的查询条件,或者只知道信息的一部分,可以借助LIKE来实现模糊查询.LIKE需要借助两个通配符: %:表示0到多个字符 _:标识单个字符 这两个通配符可以配合使用,构造灵活的匹配条件.例如查询职员姓名中第二个字符是'A'的员工信息: SELECT ename, job FROM emp WHERE ename LIKE '_A%'; 1.1.2. 使用IN和NOT IN

8.修改 按条件查询商品 分页

修改商品的原理图 jsp页面 edit.jsp <%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <HTML> <HEAD> <meta http-equiv="Content-Langua

PHP连接数据库实现多条件查询与分页功能——关于租房页面的完整实例操作

租房页面如图: 代码如下: <!DOCTYPE html><html>    <head>        <meta charset="UTF-8">        <title>租房子</title>        <script src="bootstrap/js/jquery-1.11.2.min.js"></script> //引入bootstrap前端框架的三个

2017-6-2 Linq 高级查询 (分页和组合查)、集合取交集

1.linq分页和组合查询:(用项目实战来解释) <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <he

(转)Entity Framework4.1实现动态多条件查询、分页和排序

原文:http://www.cnblogs.com/ahui/archive/2011/08/04/2127282.html EF通用的分页实现: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 /// <summary> /// 根据条件分页获得记录 /// </summary> /// <param name="where">条件</param> /// <

Linq组合查询与分页组合查询结合

1.组合查询 <div>姓名:<asp:TextBox ID="T1" runat="server"></asp:TextBox></div> <div> 性别:<asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem Text="男和女" Value=&

分页 --条件查询再分页

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-