前台界面,并在后台绑定数据
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Cars.aspx.cs" Inherits="Cars" %> <!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> </head> <body> <form id="form1" runat="server"> <div> Name:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> Brand:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> Oil: <asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem Value="=">等于</asp:ListItem> <asp:ListItem Value=">=">大于</asp:ListItem> <asp:ListItem Value="<=">小于</asp:ListItem> </asp:DropDownList> <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> Price: <asp:DropDownList ID="DropDownList2" runat="server"> <asp:ListItem Value="=">等于</asp:ListItem> <asp:ListItem Value=">=">大于</asp:ListItem> <asp:ListItem Value="<=">小于</asp:ListItem> </asp:DropDownList> <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="查询" /> <br /> <br /> <table style=" 100%; text-align: center;" border="1" cellpadding="0" cellspacing="0"> <tr style="background-color: navy; color: white;"> <td>Code</td> <td>Name</td> <td>Brand</td> <td>Time</td> <td>Oil</td> <td>Powers</td> <td>Exhaust</td> <td>Price</td> <td>Pic</td> </tr> <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <tr> <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> <td><%#Eval("Pic") %></td> </tr> </ItemTemplate> </asp:Repeater> </table> </div> </form> </body> </html>
条件查询:Lambda表达式 Repeater1.DataSource = con.Car.Where(r => r.Name == name);
比如按照Name进行精确查询
public partial class Cars : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { using (CarDataContext con = new CarDataContext()) { Repeater1.DataSource = con.Car; Repeater1.DataBind(); } } //<查询>按钮 Button1.Click += Button1_Click; } //<查询>按钮 void Button1_Click(object sender, EventArgs e) { //Name文本框 string name = txt_name.Text; //对数据库进行操作 using (CarDataContext con = new CarDataContext()) { //有条件时查询满足条件的数据 if (name.Length > 0) { Repeater1.DataSource = con.Car.Where(r => r.Name == name); } //无条件时查询全部数据 else { Repeater1.DataSource = con.Car; } Repeater1.DataBind(); } } }
多条件查询:Repeater1.DataSource = con.Car.Where(r => r.Name == name && r.Price == Convert.ToDecimal(price)).ToList();
public partial class Cars : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { using (CarDataContext con = new CarDataContext()) { Repeater1.DataSource = con.Car; Repeater1.DataBind(); } } //<查询>按钮 Button1.Click += Button1_Click; } //<查询>按钮 void Button1_Click(object sender, EventArgs e) { //Name文本框 string name = txt_name.Text; string price = txt_price.Text; //对数据库进行操作 using (CarDataContext con = new CarDataContext()) { //有条件时查询满足条件的数据 if (name.Length > 0) { Repeater1.DataSource = con.Car.Where(r => r.Name == name && r.Price == Convert.ToDecimal(price)); } //无条件时查询全部数据 else { Repeater1.DataSource = con.Car; } Repeater1.DataBind(); } } }
高级查询:
一、模糊查询(包含字符串)
Repeater1.DataSource = con.Car.Where(r => r.Name.Contains(name)).ToList();
二、开头、结尾
Repeater1.DataSource = con.Car.Where(r => r.Name.StartsWith(name)).ToList();
Repeater1.DataSource = con.Car.Where(r => r.Name.EndsWith(name)).ToList();
三、最大值、最小值
//模糊查询并绑定数据
IQueryable<Car> clist = con.Car.Where(r => r.Name.Contains(name)).AsQueryable();
Repeater1.DataSource = clist;
//展示数据个数
Label1.Text = clist.Max(r => r.Price).ToString();
//展示数据中最小的
Label2.Text = clist.Min(r => r.Price).ToString();
四、个数
//模糊查询并绑定数据
IQueryable<Car> clist = con.Car.Where(r => r.Name.Contains(name)).AsQueryable();
Repeater1.DataSource = clist;
//展示数据个数
Label1.Text = clist.Count().ToString();
五、平均值、和
平均值:Average
Label1.Text = con.Car.Average(r => r.Price).ToString();
和:Sum
Label1.Text = con.Car.Sum(r => r.Price).ToString();
六、升序、降序
升序:
Repeater1.DataSource = con.Car.OrderBy(r => r.Price);
降序:
Repeater1.DataSource = con.Car.OrderByDescending(r => r.Price);
分段排序 - ThenBy()
IQueryable<Car> clist = con.Car.AsQueryable().OrderBy(r => r.Brand).ThenBy(r => r.Oil);