Linq高级查询,分页查询及查询分页结合
一、高级查询与分页查询
1、以...开头 StartsWith
Repeater1.DataSource=con.Users.Where(r=>r.Nickname.StartsWith("李"));
Repeater1.DataBind();
2、以...结尾 EndsWith
Repeater1.DataSource=con.Users.Where(r=>r.Nickname.EndsWith("同"));
Repeater1.DataBind();
3、模糊差(包含) Contains
Repeater1.DataSource=con.Users.Where(r=>r.Nickname.Contains("蘇"));
Repeater1.DataBind();
4、个数 Count()或者Tolist().Count
Response.Write("总个数:"+con.Users.Count());
Response.Write("总个数:"+con.Users.Tolist().Count;
5、最大值 Max(r=>r.列名)
Response.Write("总个数:"+con.Users.Tolist().Max(r=>r.Ids);
6、最小值 Min(r=>r.列名)
Response.Write("总个数:"+con.Users.Tolist().Min(r=>r.Ids);
7、平均值 Average(r=>r.列名)
Response.Write("总个数:"+con.Users.Tolist().Average(r=>r.Ids);
8、求和 Sum(r=>r.列名)
Response.Write("总个数:"+con.Users.Tolist().Sum(r=>r.Ids);
9、升序 OrderBy(r=>r.列名)
Repeater1.DataSource=con.Users.Tolist().OrderBy(r=>r.Ids);
10、降序 OrderByDescending(r=>r.列名)
Repeater1.DataSource=con.Users.Tolist().OrderByDescending(r=>r.Ids);
11、分页 Skip()--跳过多少条 Take()--每页取多少条
Repeater1.DataSource=con.Users.Tolist().Skip(0).Take(PageCount) 表示第一页跳过0条,每页取PageCount条
二、查询分页结合
界面代码
<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));
}
}
Linq高级查询,分页查询及查询分页结合的更多相关文章
- Webform(Linq高级查、分页、组合查询)
一.linq高级查 1.模糊查(包含) 1 public List<User> Select(string name) 2 { 3 return con.User.Where(r => ...
- LinQ高级查询、组合查询、IQueryable集合类型
LinQ高级查询: 1.模糊查询(包含) Repeater1.DataSource = con.car.Where(r =>r.name.Contains(s)).ToList(); 2.开头 ...
- ajax分页与组合查询配合使用
使用纯HTML页与js.ajax.Linq实现分页与组合查询的配合使用 <body> <div id="top"><input type=" ...
- sql查询语句如何解析成分页查询?
我们公司主要mysql存储数据,因此也封装了比较好用mysql通用方法,然后,我们做大量接口,在处理分页查询接口,没有很好分查询方法.sql查询 语句如何解析成“分页查询”和“总统计”两条语句.可能, ...
- SpringData JPA进阶查询—JPQL/原生SQL查询、分页处理、部分字段映射查询
上一篇介绍了入门基础篇SpringDataJPA访问数据库.本篇介绍SpringDataJPA进一步的定制化查询,使用JPQL或者SQL进行查询.部分字段映射.分页等.本文尽量以简单的建模与代码进行展 ...
- Bitter.Core系列五:Bitter ORM NETCORE ORM 全网最粗暴简单易用高性能的 NETCore ORM 之 示例 分页聚联查询
Bitter.Core 在聚联/分页聚联查询的时候,采用原生的MSSQL, MYSQL 语句查询,做过复杂高级项目的人知道,原生的聚合查询代码执行效率更高,更快,更容易书写,开发量最少. 借助原生的M ...
- Webform(分页、组合查询)
一.分页 1.写查询方法: public List<Student> Select(int PageCount, int PageNumber) {//PageCount为每页显示条数,P ...
- MyBatis 动态SQL查询,多条件,分页
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...
- MVC中,查询以异步呈现,分页不用异步的解决方案
MVC中,查询以异步呈现,分页不用异步的解决方案 这种需求,用一个ASPX页面和一个ASCX分部视图就可以解决了,ASPX提供对ASCX的引用,ASCX显示列表信息,ASPX主页面提供查询功能 < ...
- sql中对查询出来的数据进行分页
当sql中存储的数据量比较大时,在web中 数据显示时都会对数据进行分页,分页不会在客户端进行分页,而是在数据库查询过程中进行了分页. sql代码: DECLARE @pageindex INT; - ...
随机推荐
- HTML入门5
格式化文本,高阶处理,接下来了解,标记引文,描述列表,计算机代码和其他文本,上下标,联系信息等数据. 学习不太知名的HTML元素来标记高级语义特征. 描述列表,也叫自定义列表,第三种类型的列表,除了u ...
- JAVA 学习日记
4. 数组 int[] in = new int[5];in[0] = 1;in[1] = 11;in[2] = 111;for(int i=0;i<in.length;i++){ System ...
- __x__(20)0907第四天__列表ul,ol,dl
列表分为: 有序列表 ul: <ul type="disc"> <li>张三</li> <li>李四</li> < ...
- [LeetCode] Friends Of Appropriate Ages 适合年龄段的朋友
Some people will make friend requests. The list of their ages is given and ages[i] is the age of the ...
- Centos7 Nagios 搭建
Nagios 是一款自动化运维工具,可以协助运维人员监控服务器的运行状况,并且拥有报警功能.本文章将介绍其安装方法和详细的配置方法. 总结 可以做资源,网络,应用服务的监控 配置上需要配置被监控的,服 ...
- Spring-day02
Annotation复习:1,Annotation:作为类型的元数据; 1,给类型加标记; 2,annotation可以添加各种类型的属性;2,Annotation的上的标记: 1),target:标 ...
- complex类
#include<iostream> #include<cmath> using namespace std; class complex{ public: complex() ...
- MFC 修改标题
1. Overwrite CMainFrame::PreCreateWindow. Clear the style FWS_ADDTOTITLE cs.style &= ~(LONG)FWS_ ...
- LeetCode 242 Valid Anagram 解题报告
题目要求 Given two strings s and t , write a function to determine if t is an anagram of s. 题目分析及思路 给出两个 ...
- React.createClass和extends Component的区别
React.createClass和extends Component的区别主要在于: 语法区别 propType 和 getDefaultProps 状态的区别 this区别 Mixins 语法区别 ...