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();
        }
    }
}
时间: 2024-11-04 00:49:35

Tingq 模糊查询 共多少条数据 最大、小、平均、求和值 升、降序的相关文章

Mybatis中文模糊查询,数据库中有数据,但无结果匹配

1.Mybatis中文模糊查询,数据库中有数据,但无结果匹配 1.1 问题描述: Mybatis采用中文关键字进行模糊查询,sql语句配置无误,数据库有该数据,且无任何报错信息,但无查询结果 1.2 解决方法: 修改数据库连接地址: url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8 其中,characterEncoding=UTF-8必须写在第一位 原文地址:https://www.cnblogs.com/caoleiCod

查询前几条数据

SQL查询前10条的方法为: 1.select top X *  from table_name --查询前X条记录,可以改成需要的数字,比如前10条. 2.select top X *  from table_name order by colum_name desc --按colum_name属性降序排序查询前X条记录,“order by” 后紧跟要排序的属性列名,其中desc表示降序,asc表示升序(默认也是升序方式). 3.select top n * from (select top 

2019年最新全国省市区街道共46462条数据(统计局MySQL数据库)

2019年最新全国省市区街道共46462条数据(统计局MySQL数据库) 查看百度网盘: 看到有很多朋友没有积分,很理解找资源费时间的心情,现已上传到百度云盘,直接下载 https://pan.baidu.com/s/1fDeEp5C-WGx-6Z1xPDjwGg 提取码:9ki4 只求给文章点个赞,让更多需要的人看到 原文地址:https://www.cnblogs.com/hfultrastrong/p/12689724.html

关于sql server远程访问Oracle数据库 OpenQuery查询返回多条数据的问题

在Sql Server远程访问Oracle 中的数据库表时: 远程语法通常为: select * from OpenQuery(Oracle链接服务器名称,‘查询语句’) eg: select * from OPENQUERY(QTX,'select * from student') 有些情况下只会返回student表一条数据 第一条 解决方法:数据源ODBC中 选择系统DNS 时,新建系统数据源 选择的对应驱动应该为Oracle Instant Client Dricver  这个驱动需要安装

Oracle查询前几条数据的方法

在Oracle中实现select top N:由于Oracle不支持select top 语句,所以在Oracle中经常是用order by 跟rownum的组合来实现select top n的查询.简单地说,实现方法如下所示:select 列名1 ...列名n from(select 列名1 ...列名n    from 表名 order by 列名1)where rownum <=N(抽出记录数)order by rownum asc 如:select id,name from (selec

mysql分组查询前n条数据

建表: CREATE TABLE hard(id INT,aa varchar(50) ,bb INT,PRIMARY key(id))insert into hard values(1,'a',9)insert into hard values(2,'a',7)insert into hard values(3,'a',8)insert into hard values(4,'a',6) insert into hard values(5,'b',2)insert into hard valu

oracle查询前N条数据的两种方式

在实际用途中,常常会要求取表中前几条纪录,就有以下两种方式来获取数据: 先排序后查询: SELECT * FROM (SELECT * FROM 表 m ORDER BY create_time DESC) WHERE ROWNUM <= 10 , 先查询后排序: SELECT *  FROM 表 m  WHERE ROWNUM <= 10 ORDER BY create_time DESC; 原文地址:https://www.cnblogs.com/zoushiyu/p/9746550.ht

linq to sql 怎么查询前 11 条数据

(from 新表 in db.books where 新表.bookid < 400 select 新表).Take(11); storeDB.Albums.OrderByDescending(a => a.OrderDetails.Count()).Take(count).ToList();数据上下文.Albums数据表.倒序排序(条件为按照各数据关联的OrderDetails数据表中数据的条数).拿记录(count条).立即执行转为list

数据库查询前10条数据

Oracle中查询 select * from table where rownum<=10; DB2中查询 select * from table fetch first 10 rows only; MySql中查询 select * from table limit 10;