【2017-06-02】Linq高级查询,实现分页组合查询。

时间:2023-03-08 16:12:21

1、以XXX开头

【2017-06-02】Linq高级查询,实现分页组合查询。

2、以XXX结尾

【2017-06-02】Linq高级查询,实现分页组合查询。

3、模糊查询

【2017-06-02】Linq高级查询,实现分页组合查询。

4、求个数

【2017-06-02】Linq高级查询,实现分页组合查询。

【2017-06-02】Linq高级查询,实现分页组合查询。

5、求最大值

【2017-06-02】Linq高级查询,实现分页组合查询。

6、求最小值

【2017-06-02】Linq高级查询,实现分页组合查询。

7、求平均值

【2017-06-02】Linq高级查询,实现分页组合查询。

8、求和

【2017-06-02】Linq高级查询,实现分页组合查询。

9、升序

【2017-06-02】Linq高级查询,实现分页组合查询。

10、降序

【2017-06-02】Linq高级查询,实现分页组合查询。

11、分页

Skip()跳过多少条

Take()取多少条

【2017-06-02】Linq高级查询,实现分页组合查询。

12、分页组合查询

界面部分

    <form id="form1" runat="server">
<div>
车名:<asp:TextBox ID="txt_carname" runat="server"></asp:TextBox>
油耗:<asp:DropDownList ID="dr_oil" runat="server">
<asp:ListItem Text="大于" Value=">"></asp:ListItem>
<asp:ListItem Text="小于" Value="<"></asp:ListItem>
<asp:ListItem Text="等于" Value="="></asp:ListItem>
<asp:ListItem Text="大于等于" Value=">="></asp:ListItem>
<asp:ListItem Text="大于等于" Value=">="></asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txt_oil" runat="server"></asp:TextBox>
价格:<asp:DropDownList ID="dr_price" runat="server">
<asp:ListItem Text="全部" Value="null"></asp:ListItem>
<asp:ListItem Text="小于30万" Value=""></asp:ListItem>
<asp:ListItem Text="30-40万" Value=""></asp:ListItem>
<asp:ListItem Text="40-50万" Value=""></asp:ListItem>
<asp:ListItem Text="大于50万" Value=""></asp:ListItem> </asp:DropDownList> <asp:Button ID="Button1" runat="server" Text="查询" />
<table style="width: 100%; background-color: navy; text-align: center">
<tr style="height: 40px; color: white;">
<td>id</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("oil") %></td>
<td><%#Eval("powers") %></td>
<td><%#Eval("price") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
当前第【 <asp:Label ID="lab_nownumber" runat="server" Text=""></asp:Label> 】页,一共【 <asp:Label ID="lab_maxnumber" runat="server" Text=""></asp:Label> 】页。
<asp:Button ID="btn_first" runat="server" Text="首页" /><asp:Button ID="btn_prev" runat="server" Text="上一页" /><asp:Button ID="btn_next" runat="server" Text="下一页" /><asp:Button ID="btn_last" runat="server" Text="尾页" />
</div>
</form>

后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class Main : System.Web.UI.Page
{
int PageCount = ;
protected void Page_Load(object sender, EventArgs e)
{
Button1.Click += Button1_Click;
btn_first.Click += btn_first_Click;
btn_next.Click += btn_next_Click;
btn_prev.Click += btn_prev_Click;
btn_last.Click += btn_last_Click;
if (!IsPostBack)
{
using (Data0216DataClassesDataContext con = new Data0216DataClassesDataContext())
{
Repeater1.DataSource = Data().Skip().Take(PageCount);
Repeater1.DataBind();
lab_maxnumber.Text = Max().ToString();
}
}
} void btn_last_Click(object sender, EventArgs e)
{
Repeater1.DataSource = Data().Skip((Max() - ) * PageCount);
Repeater1.DataBind();
lab_nownumber.Text = Max().ToString();
lab_maxnumber.Text = Max().ToString();
} void btn_prev_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(lab_nownumber.Text) - ;
if (a > )
{
Repeater1.DataSource = Data().Skip((a - ) * PageCount).Take(PageCount);
Repeater1.DataBind();
lab_nownumber.Text = a.ToString();
lab_maxnumber.Text = Max().ToString();
}
} void btn_next_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(lab_nownumber.Text) + ;
if (a <= Max())
{
Repeater1.DataSource = Data().Skip((a - ) * PageCount).Take(PageCount);
Repeater1.DataBind();
lab_nownumber.Text = a.ToString();
lab_maxnumber.Text = Max().ToString();
}
} void btn_first_Click(object sender, EventArgs e)
{
Repeater1.DataSource = Data().Skip().Take(PageCount);
Repeater1.DataBind();
lab_nownumber.Text = "";
lab_maxnumber.Text = Max().ToString();
} void Button1_Click(object sender, EventArgs e)
{
Repeater1.DataSource = Data().Skip().Take(PageCount);
Repeater1.DataBind();
lab_nownumber.Text = "";
lab_maxnumber.Text = Max().ToString();
} public List<car> Data()
{ using (Data0216DataClassesDataContext con = new Data0216DataClassesDataContext())
{
List<car> clist = con.car.ToList(); if (txt_carname.Text.Trim().Length > )
{
clist = clist.Where(r => r.name.Contains(txt_carname.Text.Trim())).ToList();
} if (txt_oil.Text.Trim().Length > )
{
if (dr_oil.SelectedValue == ">")
clist = clist.Where(r => r.oil > Convert.ToDecimal(txt_oil.Text)).ToList();
if (dr_oil.SelectedValue == "<")
clist = clist.Where(r => r.oil < Convert.ToDecimal(txt_oil.Text)).ToList();
if (dr_oil.SelectedValue == ">=")
clist = clist.Where(r => r.oil >= Convert.ToDecimal(txt_oil.Text)).ToList();
if (dr_oil.SelectedValue == "<=")
clist = clist.Where(r => r.oil <= Convert.ToDecimal(txt_oil.Text)).ToList();
if (dr_oil.SelectedValue == "=")
clist = clist.Where(r => r.oil == Convert.ToDecimal(txt_oil.Text)).ToList();
} if (dr_price.SelectedValue != "null")
{
if (dr_price.SelectedValue == "")
clist = clist.Where(r => r.price < ).ToList();
if (dr_price.SelectedValue == "")
clist = clist.Where(r => r.price >= && r.price < ).ToList();
if (dr_price.SelectedValue == "")
clist = clist.Where(r => r.price >= && r.price < ).ToList();
if (dr_price.SelectedValue == "")
clist = clist.Where(r => r.price >= ).ToList();
}
return clist;
}
}
public int Max()
{
return Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(Data().Count) / PageCount));
}
}