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

1、组合查询

 <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="查询" />
 void Button2_Click(object sender, EventArgs e)
    {
        using (StudentsDataClassesDataContext con = new StudentsDataClassesDataContext())
        {
            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(); }
            }
            Repeater1.DataSource = s;
            Repeater1.DataBind();
        }
    }

 

2、分页+组合查询

<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="Button4" 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;
        Button3.Click += Button3_Click;
        Button4.Click += Button4_Click;
    }

    void Button4_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();
        }
    }


//把组合查询封装成一个方法 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)); } }

 

转载于:https://www.cnblogs.com/snow22546/p/7115816.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值