Tingq 模糊查询 共多少条数据 最大小平均求和值 升降序
Posted 纡ゾ少︶ㄣ
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tingq 模糊查询 共多少条数据 最大小平均求和值 升降序相关的知识,希望对你有一定的参考价值。
页面代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %> <!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> <style type="text/css"> #tb1 { width: 100%; background-color: navy; text-align: center; border-spacing: 1px; } #tb1 thead td { color: white; } #tb1 tbody tr { background-color: white; } #tb1 td { padding: 5px 0; } </style> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <table id="tb1"> <thead> <tr> <td>ids</td> <td>code</td> <td>name</td> <td>oil</td> <td>powers</td> <td>price</td> </tr> </thead> <tbody> <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <tr> <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> </tbody> </table> </div> </form> </body> </html>
后台代码:
模糊查询
全部模糊查询(Contains())
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; //页面绑定数据 if (!IsPostBack) { using (DataClassesDataContext con = new DataClassesDataContext()) { Repeater1.DataSource = con.car; Repeater1.DataBind(); } } } private void Button1_Click(object sender, EventArgs e) { //全部模糊差 string s = TextBox1.Text;//获取用户输入的内容 using (DataClassesDataContext con = new DataClassesDataContext()) { //car表里name列里全部(Contains())带用户输入内容(s)的数据连接到 Repeater1 Repeater1.DataSource = con.car.Where(r => r.name.Contains(s)); Repeater1.DataBind();//绑定到 Repeater1上 //car表里油耗大于7.4并且价格大于50的(类型不相同 一个是decimal?型和double型) Repeater1.DataSource = con.car.Where(r => r.oil>7.4m && r.price>50m); Repeater1.DataBind();//绑定到 Repeater1上 } } }
开头(StartsWith())
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; //页面绑定数据 if (!IsPostBack) { using (DataClassesDataContext con = new DataClassesDataContext()) { Repeater1.DataSource = con.car; Repeater1.DataBind(); } } } private void Button1_Click(object sender, EventArgs e) { //开头模糊差 string s = TextBox1.Text;//获取用户输入的内容 using (DataClassesDataContext con = new DataClassesDataContext()) { //car表里name列里开头(StartsWith())带用户输入内容(s)的数据连接到 Repeater1 Repeater1.DataSource = con.car.Where(r => r.name.StartsWith(s)); Repeater1.DataBind();//绑定到 Repeater1上 } } }
结尾(EndsWith())
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; //页面绑定数据 if (!IsPostBack) { using (DataClassesDataContext con = new DataClassesDataContext()) { Repeater1.DataSource = con.car; Repeater1.DataBind(); } } } private void Button1_Click(object sender, EventArgs e) { //结尾模糊差 string s = TextBox1.Text;//获取用户输入的内容 using (DataClassesDataContext con = new DataClassesDataContext()) { //car表里name列里结尾(EndsWith())带用户输入内容(s)的数据连接到 Repeater1 Repeater1.DataSource = con.car.Where(r => r.name.EndsWith(s)); Repeater1.DataBind();//绑定到 Repeater1上 } } }
个数:Count
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; //页面绑定数据 if (!IsPostBack) { using (DataClassesDataContext con = new DataClassesDataContext()) { Repeater1.DataSource = con.car; Repeater1.DataBind(); } } } private void Button1_Click(object sender, EventArgs e) { string s = TextBox1.Text;//获取用户输入的内容 using (DataClassesDataContext con = new DataClassesDataContext()) { //查询一共多少条数据 (Lbel1需要的事string型 con.car.Count()是int型转换成string型) Label1.Text = con.car.Count().ToString(); //或者 Label1.Text = con.car.ToList().Count.ToString(); //查询油耗大于7.5的一共多少条数据 Label1.Text = con.car.Where(r => r.oil > 7.5m).Count().ToString(); } } }
最大值:Max(r => r.price)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; //页面绑定数据 if (!IsPostBack) { using (DataClassesDataContext con = new DataClassesDataContext()) { Repeater1.DataSource = con.car; Repeater1.DataBind(); } } } private void Button1_Click(object sender, EventArgs e) { string s = TextBox1.Text;//获取用户输入的内容 using (DataClassesDataContext con = new DataClassesDataContext()) { //查询car表里价格列里最大的值(返回的是一个值不是一个对象) Label1.Text = con.car.Max(r => r.price).ToString(); } } }
最小值:Min(r => r.price)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; //页面绑定数据 if (!IsPostBack) { using (DataClassesDataContext con = new DataClassesDataContext()) { Repeater1.DataSource = con.car; Repeater1.DataBind(); } } } private void Button1_Click(object sender, EventArgs e) { string s = TextBox1.Text;//获取用户输入的内容 using (DataClassesDataContext con = new DataClassesDataContext()) { //查询car表里价格列里最小的值(返回的是一个值不是一个对象) Label1.Text = con.car.Min(r => r.price).ToString(); } } }
平均值:Average(r => r.price)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; //页面绑定数据 if (!IsPostBack) { using (DataClassesDataContext con = new DataClassesDataContext()) { Repeater1.DataSource = con.car; Repeater1.DataBind(); } } } private void Button1_Click(object sender, EventArgs e) { string s = TextBox1.Text;//获取用户输入的内容 using (DataClassesDataContext con = new DataClassesDataContext()) { //查询car表里价格列里平均值(返回的是一个值不是一个对象) Label1.Text = con.car.Average(r => r.price).ToString(); } } }
求和:Sum(r => r.price)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; //页面绑定数据 if (!IsPostBack) { using (DataClassesDataContext con = new DataClassesDataContext()) { Repeater1.DataSource = con.car; Repeater1.DataBind(); } } } private void Button1_Click(object sender, EventArgs e) { string s = TextBox1.Text;//获取用户输入的内容 using (DataClassesDataContext con = new DataClassesDataContext()) { //查询car表里价格列里求和(返回的是一个值不是一个对象) Label1.Text = con.car.Sum(r => r.price).ToString(); } } }
升序:OrderBy(r => r.price);
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; //页面绑定数据 if (!IsPostBack) { using (DataClassesDataContext con = new DataClassesDataContext()) { Repeater1.DataSource = con.car; Repeater1.DataBind(); } } } private void Button1_Click(object sender, EventArgs e) { using (DataClassesDataContext con = new DataClassesDataContext()) { //car表里Oil列里升序排序(返回的是一个值不是一个对象) Repeater1.DataSource= con.car.OrderBy(r => r.price); Repeater1.DataBind(); } } }
降序:OrderByDescending(r => r.price)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; //页面绑定数据 if (!IsPostBack) { using (DataClassesDataContext con = new DataClassesDataContext()) { Repeater1.DataSource = con.car; Repeater1.DataBind(); } } } private void Button1_Click(object sender, EventArgs e) { using (DataClassesDataContext con = new DataClassesDataContext()) { //car表里Oil列里降序排序(返回的是一个值不是一个对象) Repeater1.DataSource= con.car.OrderByDescending(r => r.price); Repeater1.DataBind(); } } }
以上是关于Tingq 模糊查询 共多少条数据 最大小平均求和值 升降序的主要内容,如果未能解决你的问题,请参考以下文章