LINQ 组合查询 和分页查询的使用

时间:2021-11-16 02:43:35

前端代码

LINQ 组合查询 和分页查询的使用LINQ 组合查询 和分页查询的使用
<%@ Page Language="C#" AutoEventWireup="true" Debug="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>
    <script src="DatePicker/WdatePicker.js"></script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <br />
            编号:<asp:TextBox ID="code_text" style="width:50px;" runat="server"></asp:TextBox>
            汽车名:<asp:TextBox ID="carname_text" runat="server"></asp:TextBox>
            系列:<asp:TextBox ID="brand_text" runat="server"></asp:TextBox>
            时间段:<asp:TextBox ID="date_text1" class="date"  runat="server"></asp:TextBox><asp:TextBox ID="date_text2" class="date"  runat="server"></asp:TextBox><br />
            油耗:<asp:DropDownList ID="dr_oil" runat="server">
                <asp:ListItem Text="全部" Value="null"></asp:ListItem>
                 <asp:ListItem Text="大于8.0" Value="dayu8.0"></asp:ListItem>
                 <asp:ListItem Text="5.0到8.0" Value="5.0zhi8.0"></asp:ListItem>
                <asp:ListItem Text="低于5.0" Value="diyu5.0"></asp:ListItem>
               </asp:DropDownList>
            排量:<asp:TextBox ID="p_text1" runat="server"></asp:TextBox><asp:TextBox ID="p_text2" runat="server"></asp:TextBox>
            价格:<asp:TextBox ID="price_text1" runat="server"></asp:TextBox><asp:TextBox ID="price_text2" runat="server"></asp:TextBox>
            <asp:Button ID="btn_select" runat="server" Text="查询" />
            <br /><br />
            <table style="width:100%;background-color:navy;">
                <tr style="font-size:25px;color:white;">
                    <td>Ids</td>
                    <td>编号</td>
                    <td>汽车名</td>
                    <td>系列</td>
                    <td>时间</td>
                    <td>油耗</td>
                    <td>马力</td>
                    <td>排量</td>
                    <td>价格</td>
                </tr>

                <asp:Repeater ID="Repeater1" runat="server">
                    <ItemTemplate>
                        <tr style="background-color:white;">
                            <td><%#Eval("ids") %></td>
                            <td><%#Eval("code") %></td>
                            <td><%#Eval("name") %></td>
                            <td><%#Eval("brand") %></td>
                            <td><%#Eval("time") %></td>
                            <td><%#Eval("oil") %></td>
                            <td><%#Eval("powers") %></td>
                            <td><%#Eval("exhaust") %></td>
                            <td><%#Eval("price") %></td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
            </table>

            当前第[<asp:Label ID="lable_now" runat="server" Text="1"></asp:Label>]页,共[<asp:Label ID="lable_zong" runat="server" Text=""></asp:Label>]页
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            <asp:Button ID="btn_begin" runat="server" Text="首页" />
            <asp:Button ID="btn_prev" runat="server" Text="上一页" />
            <asp:Button ID="btn_next" runat="server" Text="下一页" />
            <asp:Button ID="btn_end" runat="server" Text="尾页" />
            &nbsp; &nbsp; &nbsp; &nbsp; 
            <asp:DropDownList ID="dr_jump" runat="server"></asp:DropDownList>
            <asp:Button ID="btn_jump" runat="server" Text="跳转" />
        </div>
    </form>
</body>
</html>
<script type="text/javascript">
    var date = document.getElementsByClassName("date");
    for (var i = 0; i < date.length; i++)
    {
        date[i].onfocus = function () {
            WdatePicker({ readOnly: true, maxDate: '%y-%M-%d' });
        }
    }
</script>
View Code

 

后端代码

LINQ 组合查询 和分页查询的使用LINQ 组合查询 和分页查询的使用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    //每一页 显示多少条数据   5条
    int pagecount = 5;

    #region 页面加载时 绑定的数据
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            Repeater1.DataSource = SelectToList().Take(pagecount);
            Repeater1.DataBind();
            lable_now.Text = "1";

        }

        // 绑定事件
        btn_select.Click += btn_select_Click;
        btn_begin.Click += btn_begin_Click;
        btn_end.Click += btn_end_Click;
        btn_next.Click += btn_next_Click;
        btn_prev.Click += btn_prev_Click;
        btn_jump.Click += btn_jump_Click;
    #endregion
    }

    void btn_jump_Click(object sender, EventArgs e)
    {
        int a = Convert.ToInt32(dr_jump.SelectedValue);
        if (a == 1)
        {
            btn_begin.Visible = false;
            btn_prev.Visible = false;
            btn_end.Visible = true;
            btn_next.Visible = true;
        }
        else if (a.ToString() == lable_zong.Text)
        {
            btn_end.Visible = false;
            btn_next.Visible = false;
            btn_begin.Visible = true;
            btn_prev.Visible = true;
        }
        else
        {
            btn_begin.Visible = true;
            btn_prev.Visible = true;
            btn_end.Visible = true;
            btn_next.Visible = true;
        }
         Repeater1.DataSource = SelectToList().Skip((a - 1) * pagecount).Take(pagecount);
        Repeater1.DataBind();
        lable_now.Text = a.ToString();
    }

    void btn_prev_Click(object sender, EventArgs e)
    {
        
        int a = Convert.ToInt32(lable_now.Text) - 1;
        //假设到 首页 上一页 和 首页隐藏
        if (a < 1)
        { 
        btn_begin.Visible = false;
        btn_prev.Visible = false;
        return;
        }
        btn_begin.Visible = true;
        btn_prev.Visible = true;
        btn_end.Visible = true;
        btn_next.Visible = true;
        Repeater1.DataSource = SelectToList().Skip((a - 1) * pagecount).Take(pagecount);
        Repeater1.DataBind();
        lable_now.Text = a.ToString();
    }

    void btn_next_Click(object sender, EventArgs e)
    {
        
        int a = Convert.ToInt32(lable_now.Text) + 1;
        //假设到尾页时   下一页和尾页隐藏 
        if (a > Convert.ToInt32(lable_zong.Text))
        {
            
            btn_end.Visible = false;
            btn_next.Visible = false;
            return;
        }
        btn_begin.Visible = true;
        btn_prev.Visible = true;
        btn_end.Visible = true;
        btn_next.Visible = true;
        Repeater1.DataSource = SelectToList().Skip((a - 1) * pagecount).Take(pagecount);
        Repeater1.DataBind();
        lable_now.Text = a.ToString();
    }

    void btn_end_Click(object sender, EventArgs e)
    {
        //假设到尾页时   下一页和尾页隐藏 上一页和首页显示
        btn_end.Visible = false;
        btn_next.Visible = false;
        btn_begin.Visible = true;
        btn_prev.Visible = true;
        //数据显示尾页数据
        int a = Convert.ToInt32(lable_zong.Text);       
         Repeater1.DataSource =  SelectToList().Skip((a - 1) * pagecount).Take(pagecount);
         Repeater1.DataBind();
         lable_now.Text = a.ToString();
    }

    void btn_begin_Click(object sender, EventArgs e)
    {
        //假设 到首页时  上一页和首页隐藏
        btn_begin.Visible = false;
        btn_prev.Visible = false;
        btn_end.Visible = true;
        btn_next.Visible = true;
        //数据  首页 数据绑定
        Repeater1.DataSource = SelectToList().Take(pagecount);
        Repeater1.DataBind();
        lable_now.Text = "1";
    }
   

    void btn_select_Click(object sender, EventArgs e)
    {
        
        lable_now.Text = "1";
        Repeater1.DataSource = SelectToList().Take(pagecount);
        Repeater1.DataBind();
    }

    //查询数据的方法
    public List<car> SelectToList()
    {
        using (CarDataContext con = new CarDataContext())
        {
            List<car> clist = con.car.ToList();
            //编号 code
            if (code_text.Text.Trim().Length > 0)
            {
                //包含  code
                clist = clist.Where(r => r.code.Contains(code_text.Text.Trim())).ToList();
            }
            //查汽车名
            if (carname_text.Text.Trim().Length > 0)
            {

                clist = clist.Where(r => r.name.Contains(carname_text.Text.Trim())).ToList();
            }
            //查时间段
            if (date_text1.Text.Trim().Length > 0)
            {
                clist = clist.Where(r => Convert.ToDateTime(r.time).Date >= Convert.ToDateTime(date_text1.Text).Date).ToList();

            }
            if (date_text2.Text.Trim().Length > 0)
            {
                clist = clist.Where(r => Convert.ToDateTime(r.time).Date <=Convert.ToDateTime(date_text2.Text).Date).ToList();
            }
            //查油耗  oil
            if (dr_oil.SelectedValue == "dayu8.0")
            {
                clist = clist.Where(r => Convert.ToDecimal(r.oil) >= Convert.ToDecimal(8.0)).ToList();
            }
            else if (dr_oil.SelectedValue == "5.0zhi8.0")
            {
                clist = clist.Where(r => Convert.ToDecimal(r.oil) > Convert.ToDecimal(5.0) && Convert.ToDecimal(r.oil) < Convert.ToDecimal(8.0)).ToList();
            }
            else if (dr_oil.SelectedValue == "diyu5.0")
            {
                clist = clist.Where(r => Convert.ToDecimal(r.oil) <= Convert.ToDecimal(5.0)).ToList();
            }
            //查 排量exhaus列
            if (p_text1.Text.Trim().Length > 0)
            {
                clist = clist.Where(r => Convert.ToInt32(r.exhaust) >= Convert.ToInt32(p_text1.Text.Trim())).ToList();
            }
            if (p_text2.Text.Trim().Length > 0)
            {
                clist = clist.Where(r => Convert.ToInt32(r.exhaust) <= Convert.ToInt32(p_text2.Text.Trim())).ToList();
            }
            //查价格price
            if (price_text1.Text.Trim().Length > 0)
            {
                clist = clist.Where(r => Convert.ToDecimal(r.price) >= Convert.ToDecimal(price_text1.Text.Trim())).ToList();
            }
            if (price_text2.Text.Trim().Length > 0)
            {
                clist = clist.Where(r => Convert.ToDecimal(r.price) <= Convert.ToDecimal(price_text2.Text.Trim())).ToList();
            }
            int a = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(clist.Count) / pagecount));
            lable_zong.Text = a.ToString();

            dr_jump.Items.Clear();
            for (int i = 1; i <= a; i++)
            {
                ListItem li = new ListItem(i.ToString(), i.ToString());
                dr_jump.Items.Add(li);
            }


            return clist;
        }
    }



}
View Code