Webform中linq to sql多条件查询(小练习)

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

aspx代码

 1 <body>
 2     <form id="form1" runat="server">
 3         
 4         <br />
 5         <asp:Label ID="Label1" runat="server" Text="关键字:"></asp:Label>
 6         <asp:TextBox ID="Gjz" runat="server" Font-Size="12px"></asp:TextBox>
 7 &nbsp;
 8         <asp:Label ID="Label2" runat="server" Text="价格:"></asp:Label>
 9 &nbsp;<asp:TextBox ID="price1" runat="server" Font-Size="12px" Width="52px"></asp:TextBox>
10 &nbsp;
11         <asp:Label ID="Label3" runat="server" Text=""></asp:Label>
12 &nbsp;
13         <asp:TextBox ID="price2" runat="server" Font-Size="12px" Width="52px"></asp:TextBox>
14 &nbsp;
15         <asp:Button ID="select" runat="server" Text="查询" OnClick="select_Click" />
16         <br />
17         <br />
18         <asp:Repeater ID="Repeater1" runat="server">
19             <HeaderTemplate>
20                 <table width="903" border="0" cellspacing="1" cellpadding="0" bgcolor="#6600FF">
21                   <tr style="height:25px;">
22                     <td width="260" bgcolor="#FFFFFF">名称</td>
23                     <td width="160" bgcolor="#FFFFFF">上市时间</td>
24                     <td width="160" bgcolor="#FFFFFF">油耗</td>
25                     <td width="160" bgcolor="#FFFFFF">功率</td>
26                     <td width="163" bgcolor="#FFFFFF">价格</td>
27                   </tr>
28             </HeaderTemplate>
29 
30             <ItemTemplate>
31                  <tr style="height:25px;">
32                     <td bgcolor="#FFFFFF"><%#Eval("Name") %></td>
33                     <td bgcolor="#FFFFFF"><%#Eval("Time","{0:yyyy年MM月dd日}") %></td>
34                     <td bgcolor="#FFFFFF"><%#Eval("Oil") %></td>
35                     <td bgcolor="#FFFFFF"><%#Eval("Powers") %></td>
36                     <td bgcolor="#FFFFFF"><%#Eval("Price") %></td>
37                   </tr>
38 
39             </ItemTemplate>
40 
41             <FooterTemplate>
42 
43                  </table>
44 
45             </FooterTemplate>
46 
47 
48         </asp:Repeater>
49     </form>
50 </body>

 aspx.cs代码:

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 
 8 public partial class test : System.Web.UI.Page
 9 {
10     protected void Page_Load(object sender, EventArgs e)
11     {
12         if(!IsPostBack)
13         {
14          coenctDataContext _conext = new coenctDataContext();
15            Repeater1.DataSource=  _conext.Car;
16            Repeater1.DataBind();
17         }
18     }
19 
20 
21     protected void select_Click(object sender, EventArgs e)
22     {
23         coenctDataContext _contest = new coenctDataContext();
24 
25         List<Car> list =  _contest.Car.ToList();
26         string key =Gjz.Text.ToString();
27         
28         //判断第一个条件是否为空,如果为空不操作,去判断第二个条件看是否满足;不为空,继续 继续查询
29         if (key != "")
30         {
31             list = list.Where(p => p.Name.Contains(key)).ToList();
32             //关键字变原色,用foreach去查看集合中满足条件的字
33             foreach(Car data in list )
34             {
35                 string s = data.Name.Replace(key, "<span style ='color:red;'>" + key + "</span>");
36                 data.Name = s;
37             }
38         }
39 
40         //判断第二个条件
41         string mi = price1.Text;//取text判断是否为空
42         string ma = price2.Text;//取text判断是否为空
43         
44         //先判断第二个条件中,文本框中是否为空
45         if(mi !="" && ma != "")
46         {
47             decimal min = Convert.ToDecimal(price1.Text);
48             decimal max = Convert.ToDecimal(price2.Text);
49 
50            list=    list.Where(p=>p.Price.Value>=min && p.Price<=max).ToList();
51 
52         }
53 
54         //指定数据源显示出来
55         Repeater1.DataSource = list;
56         Repeater1.DataBind();
57 
58     }
59 }

 

posted @ 2015-10-14 09:12  Franky2015  阅读(301)  评论(0编辑  收藏  举报