Linq的分页与组合查询的配合使用

1.首先使用Linq连接数据库,并扩展属性

 1 public partial class User
 2 {
 3     public string SexStr
 4     {
 5         get
 6         {
 7             string end = "<暂无>";
 8             if (this._Sex != null)
 9             {
10                 end = Convert.ToBoolean(this._Sex) ? "" : "";
11             }
12             return end;
13         }
14     }
15     public bool SexStr1
16     {
17         get
18         {
19             bool end=false;
20             
21             if (this._Sex != null)
22             {
23                 end = Convert.ToBoolean(this._Sex) ;
24             }
25             return end;
26         }
27     }
28     public string NationName
29     {
30         get
31         {
32             return this.Nation1.NationName;
33         }
34     }
35     public string BirStr
36     {
37         get
38         {
39             string end = "<暂无>";
40             if(this._Birthday!=null)
41             {
42                 end = Convert.ToDateTime(this._Birthday).ToString("yyyy年MM月dd日");
43             }
44             return end;
45         }
46     }
47     public int Age
48     {
49         get
50         {
51             int end=0;
52             if (this._Birthday != null)
53             {
54                 int y = DateTime.Now.Year;
55                 int a = Convert.ToDateTime(this._Birthday).Year;
56                 end = y-a;
57             }
58             return end;
59         }
60     }
61 }
扩展属性

2.HTML代码

<form id="form1" runat="server">
        <div id="select">
            用户名:<asp:TextBox ID="TextBox1" runat="server" Width="130"></asp:TextBox>
           性别:<asp:TextBox ID="tb_sex" runat="server" Width="60px"></asp:TextBox>&nbsp
            年龄:<asp:DropDownList ID="DropDownList3" runat="server">
                <asp:ListItem Selected="True" Value="&lt;=">&gt;=</asp:ListItem>
                <asp:ListItem Value="&gt;=">&lt;=</asp:ListItem>
            </asp:DropDownList><asp:TextBox ID="tb_age" runat="server" Width="60px"></asp:TextBox>
             &nbsp<asp:Button ID="Button2" CssClass="Button" runat="server" Text="查询" />&nbsp&nbsp<a href="Insert.aspx" target="_blank" class="link">添加</a></div> 
        
        <asp:Repeater ID="Repeater1" runat="server">
            <HeaderTemplate >
                <table style="width: 100%; background-color: #4cff00; text-align: center;">
                     <tr style="color:#ff6a00;">
                        <td>用户名</td>
                        <td>密码</td>
                        <td>昵称</td>
                        <td>性别</td>
                        <td>生日</td>
                         <td>年龄</td>
                        <td>民族</td>
                         <td>操作</td>
                    </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr style="background-color:#0ff;" class="tr_item">
                    <td><%#Eval("UserName") %></td>
                    <td><%#Eval("PassWord") %></td>
                    <td><%#Eval("NickName") %></td>
                    <td><%#Eval("SexStr") %></td>
                    <td><%#Eval("Birthday","{0:yyyy年MM月dd日}") %></td>
                    <td><%#Eval("Age") %></td>
                    <td><%#Eval("NationName") %></td>
                    <td><a href="update.aspx?un=<%#Eval("UserName") %>" target="_blank" class="link">修改</a>&nbsp;&nbsp;
                        <a id="lian" href="delete.aspx?un=<%#Eval("UserName") %>" οnclick="return del()" class="link">删除</a></td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>
        //隐藏域放用户输入的多条件查询内容
        <asp:HiddenField ID="HiddenField1" runat="server" />
        <asp:HiddenField ID="HiddenField2" runat="server" />
        <asp:HiddenField ID="HiddenField3" runat="server" />
         <div id="aa">当前第&nbsp<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>&nbsp页&nbsp&nbsp共&nbsp<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>&nbsp页
            <asp:Button ID="btn_first" runat="server" CssClass="ye" Text="首页" />&nbsp<asp:Button ID="btn_prev" runat="server" Text="上一页" CssClass="ye" />&nbsp<asp:Button ID="btn_next" runat="server" Text="下一页" CssClass="ye" />&nbsp<asp:Button ID="btn_end" runat="server" Text="末页" CssClass="ye"/>
            <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"></asp:DropDownList></div>
    </form>
View Code

3、C#功能实现

 1 private IEnumerable<global::User> sel(WebDataContext con)
 2     {
 3         var All = con.User.AsEnumerable();
 4         if (HiddenField1.Value.Length > 0)
 5         {
 6             var namelist = con.User.Where(r => r.UserName.Contains(TextBox1.Text.Trim()));
 7 
 8             All = All.Intersect(namelist);
 9         }
10 
11         if (HiddenField2.Value == "" || HiddenField2.Value == "")
12         {
13             var sexlist = con.User.Where(r => Convert.ToBoolean(r.Sex) == (tb_sex.Text.Trim() == "" ? true : false));
14 
15             All = All.Intersect(sexlist);
16         }
17 
18         if (HiddenField3.Value.Length > 0)
19         {
20             int nowyear = DateTime.Now.Year;
21             try
22             {
23                 int age = Convert.ToInt32(tb_age.Text.Trim());
24                 int g = nowyear - age;
25                 DateTime d = Convert.ToDateTime(g.ToString() + "-1-1");
26                 if (DropDownList3.SelectedValue == ">=")
27                 {
28                     var agelist = con.User.Where(r => Convert.ToDateTime(r.Birthday) >= d);
29                     All = All.Intersect(agelist);
30 
31                 }
32                 else
33                 {
34                     DateTime dd = Convert.ToDateTime(g.ToString() + "-12-31");
35                     var agelist = con.User.Where(r => Convert.ToDateTime(r.Birthday) <= dd);
36                     All = All.Intersect(agelist);
37 
38                 }
39             }
40             catch
41             {
42             }
43         }
44         return All;
45     }
查询方法
 1 int Pagecount = 4;
 2     protected void Page_Load(object sender, EventArgs e)
 3     {
 4         if (!IsPostBack)
 5         {
 6             using (WebDataContext con = new WebDataContext())
 7             {
 8                 var All = sel(con);
 9                 Repeater1.DataSource = All.Take(Pagecount).ToList();
10                 Repeater1.DataBind();
11             }               
12             Label2.Text = "1";//当前页
13             Label3.Text = MaxPageNumber().ToString();
14             for (int i = 1; i <= MaxPageNumber();i++ )
15             {
16                 DropDownList2.Items.Add(new ListItem(i.ToString(), i.ToString()));
17             }
18         }
19    
20     }
初加载
 1 void Button2_Click(object sender, EventArgs e)//查询按钮
 2     {
 3         HiddenField1.Value = TextBox1.Text.Trim();
 4         HiddenField2.Value = tb_sex.Text.Trim();
 5         HiddenField3.Value = tb_age.Text.Trim();
 6         using (WebDataContext con = new WebDataContext())
 7         {
 8             var All = sel(con);
 9             Repeater1.DataSource = All.Take(Pagecount).ToList();
10             Repeater1.DataBind();
11             Label2.Text = "1";
12             Label3.Text = MaxPageNumber().ToString();
13             DropDownList2.Items.Clear();
14             for (int i = 1; i <= MaxPageNumber(); i++)
15             {
16                 DropDownList2.Items.Add(new ListItem(i.ToString(), i.ToString()));
17             }
18     }
19     }
查询按钮点击
1 void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
2     {
3         using (WebDataContext con = new WebDataContext())
4         {
5             var All = sel(con);
6             Repeater1.DataSource = All.Skip(Pagecount * (Convert.ToInt32(DropDownList2.SelectedValue) - 1)).Take(Pagecount).ToList();
7             Repeater1.DataBind();
8         }
9         Label2.Text =DropDownList2.SelectedValue;    }
快捷跳转
 1 void btn_first_Click(object sender, EventArgs e)
 2     {
 3         using (WebDataContext con = new WebDataContext())
 4         {
 5             var All = sel(con);
 6             Repeater1.DataSource = All.Take(Pagecount).ToList();
 7             Repeater1.DataBind();
 8         }
 9         Label2.Text ="1";
10         DropDownList2.SelectedValue = "1";
11     }
首页
 1 void btn_end_Click(object sender, EventArgs e)
 2     {
 3         using (WebDataContext con = new WebDataContext())
 4         {
 5             var All = sel(con);
 6             Repeater1.DataSource = All.Skip(Pagecount * (Convert.ToInt32(Label3.Text) - 1)).Take(Pagecount).ToList();
 7             Repeater1.DataBind();   
 8         }
 9         Label2.Text = Label3.Text;
10         DropDownList2.SelectedValue = Label3.Text;
11     }
末页
 1 void btn_prev_Click(object sender, EventArgs e)
 2     {
 3         int NowNumber = Convert.ToInt32(Label2.Text) -1;
 4         if (NowNumber <1)
 5         {
 6             return;
 7         }
 8         using (WebDataContext con = new WebDataContext())
 9         {
10             var All = sel(con);
11             Repeater1.DataSource = All.Skip(Pagecount * (NowNumber - 1)).Take(Pagecount).ToList();
12             Repeater1.DataBind();
13         }
14 
15         Label2.Text = NowNumber.ToString();
16         DropDownList2.SelectedValue = NowNumber.ToString();
17     }
上一页
 1 void btn_next_Click(object sender, EventArgs e)
 2     {
 3         int NowNumber = Convert.ToInt32(Label2.Text) + 1;
 4         if (NowNumber > Convert.ToInt32(Label3.Text))
 5         {
 6             return;
 7         }
 8         using (WebDataContext con = new WebDataContext())
 9         {
10             var All = sel(con);
11             Repeater1.DataSource = All.Skip(Pagecount * (NowNumber - 1)).Take(Pagecount).ToList();
12             Repeater1.DataBind();
13         }
14 
15         Label2.Text = NowNumber.ToString();
16         DropDownList2.SelectedValue = NowNumber.ToString();
17 
18     }
下一页
 1 public int MaxPageNumber()
 2     {
 3         using (WebDataContext con = new WebDataContext())
 4         {
 5             var All = sel(con);
 6             double nu = All.ToList().Count / (Pagecount * 1.0);
 7             int num = Convert.ToInt32(Math.Ceiling(nu));
 8             return num;
 9         }        
10     }
计算总页数

 

转载于:https://www.cnblogs.com/1711643472qq/p/6179609.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值