Webform中linq to sql多条件查询(小练习)
多条件查询:逐条判断,从第一个条件开始判断,如果满足,取出放入集合,再从集合中查询第二个条件。。。
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>
-
- <asp:Label ID="Label2" runat="server" Text="价格:"></asp:Label>
- <asp:TextBox ID="price1" runat="server" Font-Size="12px" Width="52px"></asp:TextBox>
-
- <asp:Label ID="Label3" runat="server" Text="到"></asp:Label>
-
- <asp:TextBox ID="price2" runat="server" Font-Size="12px" Width="52px"></asp:TextBox>
-
- <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多条件查询(小练习)的更多相关文章
- Mybatis中动态SQL多条件查询
Mybatis中动态SQL多条件查询 mybatis中用于实现动态SQL的元素有: if:用if实现条件的选择,用于定义where的字句的条件. choose(when otherwise)相当于Ja ...
- util-C# 复杂条件查询(sql 复杂条件查询)查询解决方案
ylbtech-funcation-util: C# 复杂条件查询(sql 复杂条件查询)查询解决方案 C# 复杂条件查询(sql 复杂条件查询)查询解决方案 1.A,Ylbtech.Model返回 ...
- mysql进阶(五)数据表中带OR的多条件查询
MySQL数据表中带OR的多条件查询 OR关键字可以联合多个条件进行查询.使用OR关键字时: 条件 1) 只要符合这几个查询条件的其中一个条件,这样的记录就会被查询出来. 2) 如果不符合这些查询条件 ...
- Linq To Sql 语法 子查询 & In & Join
子查询 描述:查询订单数超过5的顾客信息 查询句法: var 子查询 =from cin ctx.Customers where ...
- LINQ系列:LINQ to SQL Where条件
1. 单一条件查询 var expr = context.Products .Where(p => p.ProductName == "LINQ to SQL"); SELE ...
- SQL 多条件查询
网上有不少人提出过类似的问题:“看到有人写了WHERE 1=1这样的SQL,到底是什么意思?”.其实使用这种用法的开发人员一般都是在使用动态组装的SQL.让我们想像如下的场景:用户要求提供一个灵活的查 ...
- EFCore扩展:IQueryable(linq)或sql执行的查询缓存与清理
前言 上一篇讲述了执行sql和配置的一些功能,这篇说明IQueryable(linq)或执行sql的查询缓存与清理,包括扩展到将缓存存储到Redis中. 扩展类库源码: github:https:// ...
- SQL 变量 条件查询 插入数据
(本文只是总结网络上的教程) 在操作数据库时 SQL语句中难免会用到变量 比如 在條件值已知的情況下 INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值 ...
- qt sql多重条件查询简便方法
转载请注明出处:http://www.cnblogs.com/dachen408/p/7457312.html 程序设计过程中,经常要涉及到查询,并且有很多条件,且条件可为空,如果逐个判断,会有很多情 ...
随机推荐
- JS知识点摘记(一)
JavaScript:基于对象和事件的脚本语言 特点: 安全性:不允许直接访问本地硬盘,可做的就是信息的动态交互 跨平台性:只要可以解析JS的浏览器就可执行,与平台无关 JavaScript与Java ...
- WINDOWS硬件通知应用程序的常方法(五种方式:异步过程调用APC,事件方式VxD,消息方式,异步I/O方式,事件方式WDM)
摘要:在目前流行的Windows操作系统中,设备驱动程序是操纵硬件的最底层软件接口.为了共享在设备驱动程序设计过程中的经验,给出设备驱动程序通知应用程序的5种方法,详细说明每种方法的原理和实现过程,并 ...
- c# 多显示器设置主屏幕(Set primary screen for multiple monitors)
原文 http://www.cnblogs.com/coolkiss/archive/2013/09/18/3328854.html 经过google加各种百度,终于找到了一个有效的解决方案,下面是两 ...
- Siverlight+WCF+Nhibernate 开发之旅(一)
最近正在开发sl程序,考虑了很久,参考了一些框架,令人头疼的数据访问层最终选择wcf+nhibernate,至于为什么选择wcf和nh,个人参考了其他的框架感觉这两者结合从开发效率和便捷性方面比其他的 ...
- flash跨域策略文件crossdomain.xml
flash在跨域时唯一的限制策略就是crossdomain.xml文件,该文件限制了flash是否可以跨域读写数据以及允许从什么地方跨域读写数据. 位于www.a.com域中的SWF文件要访问www. ...
- Netfilter-packet-flow.svg
调试网络的方法:(Debugging the kernel using Ftrace) $ watch -n1 -d sudo cat /proc/net/snmp$ watch -n1 -d su ...
- 【Cocos2d TestCpp实例模仿一】-- ActionsTest
转载请注明出处:http://blog.csdn.net/oyangyufu/article/details/25252539 CCActionInterval(持续性动作) 位置性变化动作以To结束 ...
- 7. Reverse Integer
1. 问题描述 Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321 cl ...
- WCF:System.Security.Cryptography.CryptographicException: 密钥集不存在
WCF使用IIS部署时,使用x509证书验证,在创建证书并正确配置程序后,访问出现错误提示: System.Security.Cryptography.CryptographicException: ...
- symfony2-创建提交表单生成数据过程
一.”一对多“关系 表shop(一)