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-width: 1000px;
}

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

table {
    width: 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类

时间: 2025-01-13 00:08:39

linq高级查与分页的相关文章

Webform(Linq高级查、分页、组合查询)

Linq高级查:字符串:模糊查(包含):con.car.Where(r => r.name.Contains(name)).ToList();以什么开头:con.car.Where(r => r.name.StartsWith(name)).ToList();以什么结尾:con.car.Where(r => r.name.EndsWith(name)).ToList(); 数值:个数:.Count()最大值:con.car.Max(r => r.price);最小值:con.car

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

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

2017年12月14日 LinQ高级查&amp;&amp;Asp.net WebForm Asp.net MVC

LinQ的高级查询用法 开头:StartsWith()结尾:EndsWith()模糊:Contains() 个数:Count最大值:Max(r => r.price)最小值:Min(r => r.price)平均值:Average(r => r.price)求和:Sum(r => r.price) 升序:OrderBy(r => r.price);降序:OrderByDescending(r => r.price) 分页:Skip(从第几项开始).Take(取几个) A

Linq高级查询,分页查询及查询分页结合

1.以...开头    StartsWith Repeater1.DataSource=con.Users.Where(r=>r.Nickname.StartsWith("李")); Repeater1.DataBind(); 2.以...结尾     EndsWith Repeater1.DataSource=con.Users.Where(r=>r.Nickname.EndsWith("同")); Repeater1.DataBind(); 3.模糊

LinQ 高级查询

Linq高级查:字符串:模糊查(包含):con.car.Where(r => r.name.Contains(name)).ToList();以什么开头:con.car.Where(r => r.name.StartsWith(name)).ToList();以什么结尾:con.car.Where(r => r.name.EndsWith(name)).ToList(); 数值:个数:.Count()最大值:con.car.Max(r => r.price);最小值:con.car

linq高级查询

Linq高级查 开头: Repeater1.DataSource = con.Users.Where(r => r.NickName.StartsWith("值")); 结尾: Repeater1.DataSource = con.Users. Where( r => r.NickName.EndsWith("值")); 模糊查(包含):Repeater1.DataSource = con.Users.Where(r => r.NickName.C

SpringBoot JPA实现增删改查、分页、排序、事务操作等功能

今天给大家介绍一下SpringBoot中JPA的一些常用操作,例如:增删改查.分页.排序.事务操作等功能.下面先来介绍一下JPA中一些常用的查询操作: //And --- 等价于 SQL 中的 and 关键字,比如 findByHeightAndSex(int height,char sex): public List<User> findByHeightAndSex(int height,char sex); // Or --- 等价于 SQL 中的 or 关键字,比如 findByHeig

Linq多表链接分页,Select new{&quot;需要的字段列..&quot;},配合杨涛Mvcpager,前台遍历展示自定义字段

题前:,Select new{"需要的字段列.."}好处,减少不必要数据的查询,尤其是分布式的时候,网络再不好的情况下,而不必要的数据又很多,Select new{"需要的字段列.."}好处明显 如题,Linq多表链接分页,Select new{"需要的字段列.."},配合杨涛Mvcpager,前台遍历展示自定义字段 (1)  我的例子是,(Tb_Mnager)管理员表与(Tb_Role)角色表联查,返回两个表部分字段的组合 如下控制器代码: p