Linq的分页与组合查询的配合使用
1.首先使用Linq连接数据库,并扩展属性
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<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>  年龄:<asp:DropDownList ID="DropDownList3" runat="server"> <asp:ListItem Selected="True" Value="<=">>=</asp:ListItem> <asp:ListItem Value=">="><=</asp:ListItem> </asp:DropDownList><asp:TextBox ID="tb_age" runat="server" Width="60px"></asp:TextBox>  <asp:Button ID="Button2" CssClass="Button" runat="server" Text="查询" />  <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> <a id="lian" href="delete.aspx?un=<%#Eval("UserName") %>" onclick="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">当前第 <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label> 页  共 <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label> 页 <asp:Button ID="btn_first" runat="server" CssClass="ye" Text="首页" /> <asp:Button ID="btn_prev" runat="server" Text="上一页" CssClass="ye" /> <asp:Button ID="btn_next" runat="server" Text="下一页" CssClass="ye" /> <asp:Button ID="btn_end" runat="server" Text="末页" CssClass="ye"/> <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"></asp:DropDownList></div> </form>
3、C#功能实现
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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; }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }