linq高级查与分页

前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LinQ数据显示.aspx.cs" Inherits="LinQ数据显示" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <link href="css.css" rel="stylesheet" />
</head>
<body>
    <form id="form1" runat="server">
        <div id="chaxun">
            <br />
            姓名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <br />
            <br />
            民族:<asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem Selected="True">=请选择=</asp:ListItem>
                <asp:ListItem Value="N001">汉族</asp:ListItem>
                <asp:ListItem Value="N002">满族</asp:ListItem>
                <asp:ListItem Value="N003">藏族</asp:ListItem>
            </asp:DropDownList>
             <br />
            <br />
            生日:<asp:DropDownList ID="DropDownList2" runat="server" AppendDataBoundItems="True">
                <asp:ListItem Selected="True">=请选择=</asp:ListItem>
            </asp:DropDownList>年
            <br />
            <br />
            科目:<asp:DropDownList ID="DropDownList3" runat="server">
                <asp:ListItem Value="Sub001">语文</asp:ListItem>
                <asp:ListItem Value="Sub002">数学</asp:ListItem>
                <asp:ListItem Value="Sub003">英语</asp:ListItem>
                <asp:ListItem Selected="True">=请选择=</asp:ListItem>
            </asp:DropDownList>
            <br />
            <br />
            成绩:<asp:DropDownList ID="DropDownList4" runat="server">
                <asp:ListItem Selected="True">=请选择=</asp:ListItem>
                <asp:ListItem><</asp:ListItem>
                <asp:ListItem>=</asp:ListItem>
                <asp:ListItem>></asp:ListItem>
                <asp:ListItem><=</asp:ListItem>
                <asp:ListItem>>=</asp:ListItem>
            </asp:DropDownList>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            <br />
            <br />
            <asp:Button ID="Button1" runat="server" Text="查询" Height="20px" Width="50px" />
            <br />
            <br />
            <br />
        </div>
        <asp:Repeater ID="Repeater1" runat="server">
            <HeaderTemplate>
                <table>
                    <tr id="head">
                        <td>学生编号</td>
                        <td>学生姓名</td>
                        <td>民族</td>
                        <td>生日</td>
                        <td>科目</td>
                        <td>成绩</td>
                    </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr class="mian">
                    <td><%#Eval("Student_Code") %></td>
                    <td><%#Eval("StudentName") %></td>
                    <td><%#Eval("NationName") %></td>
                    <td><%#Eval("StudentBirthday","{0:yyyy年MM月dd日}") %></td>
                    <td><%#Eval("SubjectName") %></td>
                    <td><%#Eval("Score") %></td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>
        <br />
                    
        当前是第【<asp:Label ID="Label1" runat="server" Text="1"></asp:Label>】页  
        <asp:LinkButton ID="LinkButton1" runat="server">上一页</asp:LinkButton>
         
        <asp:LinkButton ID="LinkButton2" runat="server">下一页</asp:LinkButton>
    </form>
</body>
</html>

CSS代码:

* {
    margin: 0px;
    padding: 0px;
}

body {
    min- 1000px;
}

#xuanze {
    position: relative;
     100%;
    height: 50px;
    line-height: 50px;
}

table {
     100%;
    text-align: center;
    background-color: navy;
}

#head {
    color: white;
}

.mian {
    background-color: white;
}

td {
    padding: 5px;
}

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class LinQ数据显示 : System.Web.UI.Page
{
    dataDataContext con = new dataDataContext();

    List<Linq_Score> score = new List<Linq_Score>();

    int count = 3;

    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Click += Button1_Click;
        LinkButton1.Click += LinkButton1_Click;
        LinkButton2.Click += LinkButton2_Click;

        if (!IsPostBack)
        {
            //加载生日
            fangfa(1900, DateTime.Now.Year, DropDownList2);//年

            score = con.Linq_Score.ToList();

            Repeater1.DataSource = sel().Skip(0).Take(count);
            Repeater1.DataBind();

            Label1.Text = "1";

            LinkButton1.Enabled = false;
        }
    }

    //下一页
    void LinkButton2_Click(object sender, EventArgs e)
    {
        score = sel();

        int pagenum = Convert.ToInt32(Label1.Text);
        pagenum++;

        Repeater1.DataSource = sel().Skip((pagenum - 1) * count).Take(count);
        Repeater1.DataBind();

        Label1.Text = pagenum.ToString();

        LinkButton1.Enabled = true;

        if (pagenum == max(score))
        {
            LinkButton2.Enabled = false;
        }
    }

    //上一页
    void LinkButton1_Click(object sender, EventArgs e)
    {
        score = sel();

        int pagenum = Convert.ToInt32(Label1.Text);
        pagenum--;

        Repeater1.DataSource = sel().Skip((pagenum - 1) * count).Take(count);
        Repeater1.DataBind();

        Label1.Text = pagenum.ToString();

        LinkButton2.Enabled = true;

        if (Label1.Text == "1")
        {
            LinkButton1.Enabled = false;
        }
    }


    //最大页数
    public int max(List<Linq_Score> li)
    {
        return Convert.ToInt32(Math.Ceiling(sel().Count / 3.0));
    }

    //查询按钮的点击事件
    void Button1_Click(object sender, EventArgs e)
    {
        score = sel();

        int pagenum = 1;

        Repeater1.DataSource = sel().Skip((pagenum - 1) * count).Take(count);
        Repeater1.DataBind();

        if (max(score) == 1)
        {
            LinkButton1.Enabled = false;
            LinkButton2.Enabled = false;
        }
        else
        {
            LinkButton1.Enabled = false;
            LinkButton2.Enabled = true;
        }
    }


    /// <summary>
    /// 查询出来的集合
    /// </summary>
    /// <returns></returns>
    public List<Linq_Score> sel()
    {
        var list1 = con.Linq_Score.AsQueryable();
        var list2 = con.Linq_Score.AsQueryable();
        var list3 = con.Linq_Score.AsQueryable();
        var list4 = con.Linq_Score.AsQueryable();
        var list5 = con.Linq_Score.AsQueryable();

        //姓名
        if (TextBox1.Text.Trim().Length > 0)
        {
            list1 = list1.Where(r => r.Linq_Student.Student_Name == TextBox1.Text.Trim());
        }

        //民族
        if (DropDownList1.SelectedItem.Text != "=请选择=")
        {
            list2 = list2.Where(r => r.Linq_Student.Linq_Nation.Nation_Name == DropDownList1.SelectedItem.Text);
        }

        //生日
        if (DropDownList2.SelectedItem.Text != "=请选择=")
        {
            list3 = list3.Where(r => r.Linq_Student.Student_Birthday.Value.Year.ToString() == DropDownList2.SelectedItem.Text);
        }

        //科目
        if (DropDownList3.SelectedItem.Text != "=请选择=")
        {
            list4 = list4.Where(r => r.Linq_Subject.Subject_Name == DropDownList3.SelectedItem.Text);
        }

        //成绩
        if (DropDownList4.SelectedItem.Text != "=请选择=")
        {
            if (DropDownList4.SelectedItem.Text == "<")
            {
                list5 = list5.Where(r => r.Score < Convert.ToDecimal(TextBox2.Text));
            }
            else if (DropDownList4.SelectedItem.Text == "=")
            {
                list5 = list5.Where(r => r.Score == Convert.ToDecimal(TextBox2.Text));
            }
            else if (DropDownList4.SelectedItem.Text == ">")
            {
                list5 = list5.Where(r => r.Score > Convert.ToDecimal(TextBox2.Text));
            }
            else if (DropDownList4.SelectedItem.Text == "<=")
            {
                list5 = list5.Where(r => r.Score <= Convert.ToDecimal(TextBox2.Text));
            }
            else if (DropDownList4.SelectedItem.Text == ">=")
            {
                list5 = list5.Where(r => r.Score >= Convert.ToDecimal(TextBox2.Text));
            }
        }

        return list1.Intersect(list2).Intersect(list3).Intersect(list4).Intersect(list5).ToList();
    }

    public void fangfa(int start, int end, DropDownList ddl)
    {
        for (int i = start; i <= end; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString();
            li.Value = i.ToString();

            ddl.Items.Add(li);
        }
    }
}

用LinQ类

原文地址:https://www.cnblogs.com/123lucy/p/5748370.html