多条件查询:逐条判断,从第一个条件开始判断,如果满足,取出放入集合,再从集合中查询第二个条件。。。

aspx代码

 <body>
<form id="form1" runat="server"> <br />
<asp:Label ID="Label1" runat="server" Text="关键字:"></asp:Label>
<asp:TextBox ID="Gjz" runat="server" Font-Size="12px"></asp:TextBox>
&nbsp;
<asp:Label ID="Label2" runat="server" Text="价格:"></asp:Label>
&nbsp;<asp:TextBox ID="price1" runat="server" Font-Size="12px" Width="52px"></asp:TextBox>
&nbsp;
<asp:Label ID="Label3" runat="server" Text="到"></asp:Label>
&nbsp;
<asp:TextBox ID="price2" runat="server" Font-Size="12px" Width="52px"></asp:TextBox>
&nbsp;
<asp:Button ID="select" runat="server" Text="查询" OnClick="select_Click" />
<br />
<br />
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table width="" border="" cellspacing="" cellpadding="" bgcolor="#6600FF">
<tr style="height:25px;">
<td width="" bgcolor="#FFFFFF">名称</td>
<td width="" bgcolor="#FFFFFF">上市时间</td>
<td width="" bgcolor="#FFFFFF">油耗</td>
<td width="" bgcolor="#FFFFFF">功率</td>
<td width="" bgcolor="#FFFFFF">价格</td>
</tr>
</HeaderTemplate> <ItemTemplate>
<tr style="height:25px;">
<td bgcolor="#FFFFFF"><%#Eval("Name") %></td>
<td bgcolor="#FFFFFF"><%#Eval("Time","{0:yyyy年MM月dd日}") %></td>
<td bgcolor="#FFFFFF"><%#Eval("Oil") %></td>
<td bgcolor="#FFFFFF"><%#Eval("Powers") %></td>
<td bgcolor="#FFFFFF"><%#Eval("Price") %></td>
</tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater>
</form>
</body>

 aspx.cs代码:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
coenctDataContext _conext = new coenctDataContext();
Repeater1.DataSource= _conext.Car;
Repeater1.DataBind();
}
} protected void select_Click(object sender, EventArgs e)
{
coenctDataContext _contest = new coenctDataContext(); List<Car> list = _contest.Car.ToList();
string key =Gjz.Text.ToString(); //判断第一个条件是否为空,如果为空不操作,去判断第二个条件看是否满足;不为空,继续 继续查询
if (key != "")
{
list = list.Where(p => p.Name.Contains(key)).ToList();
//关键字变原色,用foreach去查看集合中满足条件的字
foreach(Car data in list )
{
string s = data.Name.Replace(key, "<span style ='color:red;'>" + key + "</span>");
data.Name = s;
}
} //判断第二个条件
string mi = price1.Text;//取text判断是否为空
string ma = price2.Text;//取text判断是否为空 //先判断第二个条件中,文本框中是否为空
if(mi !="" && ma != "")
{
decimal min = Convert.ToDecimal(price1.Text);
decimal max = Convert.ToDecimal(price2.Text); list= list.Where(p=>p.Price.Value>=min && p.Price<=max).ToList(); } //指定数据源显示出来
Repeater1.DataSource = list;
Repeater1.DataBind(); }
}

Webform中linq to sql多条件查询(小练习)的更多相关文章

  1. Mybatis中动态SQL多条件查询

    Mybatis中动态SQL多条件查询 mybatis中用于实现动态SQL的元素有: if:用if实现条件的选择,用于定义where的字句的条件. choose(when otherwise)相当于Ja ...

  2. util-C# 复杂条件查询(sql 复杂条件查询)查询解决方案

    ylbtech-funcation-util:  C# 复杂条件查询(sql 复杂条件查询)查询解决方案 C# 复杂条件查询(sql 复杂条件查询)查询解决方案 1.A,Ylbtech.Model返回 ...

  3. mysql进阶(五)数据表中带OR的多条件查询

    MySQL数据表中带OR的多条件查询 OR关键字可以联合多个条件进行查询.使用OR关键字时: 条件 1) 只要符合这几个查询条件的其中一个条件,这样的记录就会被查询出来. 2) 如果不符合这些查询条件 ...

  4. Linq To Sql 语法 子查询 & In & Join

    子查询 描述:查询订单数超过5的顾客信息 查询句法: var 子查询 =from cin ctx.Customers                    where                  ...

  5. LINQ系列:LINQ to SQL Where条件

    1. 单一条件查询 var expr = context.Products .Where(p => p.ProductName == "LINQ to SQL"); SELE ...

  6. SQL 多条件查询

    网上有不少人提出过类似的问题:“看到有人写了WHERE 1=1这样的SQL,到底是什么意思?”.其实使用这种用法的开发人员一般都是在使用动态组装的SQL.让我们想像如下的场景:用户要求提供一个灵活的查 ...

  7. EFCore扩展:IQueryable(linq)或sql执行的查询缓存与清理

    前言 上一篇讲述了执行sql和配置的一些功能,这篇说明IQueryable(linq)或执行sql的查询缓存与清理,包括扩展到将缓存存储到Redis中. 扩展类库源码: github:https:// ...

  8. SQL 变量 条件查询 插入数据

    (本文只是总结网络上的教程) 在操作数据库时 SQL语句中难免会用到变量 比如 在條件值已知的情況下 INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值 ...

  9. qt sql多重条件查询简便方法

    转载请注明出处:http://www.cnblogs.com/dachen408/p/7457312.html 程序设计过程中,经常要涉及到查询,并且有很多条件,且条件可为空,如果逐个判断,会有很多情 ...

随机推荐

  1. Android应用中使用自定义文字

    在Android系统中可以很方便的修改字体样式.系统提供了三种样式,分别是sans,serif,monospace.可以在xml布局文件中通过 android:typeface="[sans ...

  2. 折腾slidingmenu

    转自自己jekyll博客 alanslab.cn 第一次用gimp做这么大工程,出乎意料,蛮好用的.以前ps倒是蛮熟练的,只摸 过两下gimp,感觉望而生畏.今天硬着头皮折腾了一阵子,发现最起码上图可 ...

  3. applet部署,无需修改客户端设置。

    1  开发applet程序,编译成jar包 2  给jar包做数字签名: (1).用keytool生成密钥: keytool -genkey -keystore myapplet.keystore - ...

  4. iphone5升级到iOS7时出现“This device isn't eligible for the requested build”错误

    因为工作的需要我需要把自己的手机升级到iOS7,安装苹果的升级顺序总是报This device isn't eligible for the requested build错误,搜索相关的文章我的错误 ...

  5. window.open打开新页面,并将本页数据用过url传递到打开的页面;需要两个页面;

    页面1 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8 ...

  6. ##DAY13——可视化编程之XIB

    ##DAY13——可视化编程之XIB 1.关联控件 2.关联事件 3.关联手势 4.关联代理 这个时候即使不给控制器用下面方法添加代理,代理方法也是可以使用的,只是没有方法提示: 其他重要地方: #i ...

  7. 155Min Stack

    题目地址:155Min Stack 最近为了提高数据结构和算法能力,保证每天一到leetcode的题目.从easy开始,感觉这道题目还蛮好,记录一下. 题目大意:就是维护一个栈,获得栈中元素的的最小值 ...

  8. Problem F: Exponentiation大数求幂

    DescriptionProblems involving the computation of exact values of very large magnitude and precision ...

  9. xhprof failed to execute cmd: " dot -Tpng". stderr: `sh: dot: command not found '

    wget http://www.graphviz.org/pub/graphviz/ARCHIVE/graphviz-2.28.0.tar.gz tar xzvf graphviz-2.28.0.ta ...

  10. easyui给select控件绑定change事件

    一般的做法是使用jQuery来绑定,例如: $("#id").change(function(){ alert("change事件绑定"); }); 当给sel ...