前台界面,並在后台綁定數據

<%@ 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="width: 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);