Multi-Criteria Query: Judge by article, from the first condition to judge, if satisfied, take out into the collection, and then query the second condition from the collection ...
aspx code :
1<body>2<form id="Form1"runat="Server">3 4<br/>5<asp:label id="Label1"runat="Server"text="Key Words:"></asp:Label>6<asp:textbox id="GJZ"runat="Server"Font-size="12px"></asp:TextBox>7 8<asp:label id="Label2"runat="Server"text="Price:"></asp:Label>9 <asp:textbox id="Price1"runat="Server"Font-size="12px"Width="52px"></asp:TextBox>Ten One<asp:label id="Label3"runat="Server"text="to the"></asp:Label> A -<asp:textbox id="Price2"runat="Server"Font-size="12px"Width="52px"></asp:TextBox> - the<asp:button id="Select"runat="Server"text="Enquiry"onclick="Select_click"/> -<br/> -<br/> -<asp:repeater id="Repeater1"runat="Server"> +<HeaderTemplate> -<table width="903"Border="0"cellspacing="1"cellpadding="0"Bgcolor="#6600FF"> +<tr style="height:25px;"> A<TD width="260"Bgcolor="#FFFFFF"> Name </td> at<TD width=" the"Bgcolor="#FFFFFF"> Time to Market </td> -<TD width=" the"Bgcolor="#FFFFFF"> Fuel Consumption </td> -<TD width=" the"Bgcolor="#FFFFFF"> Power </td> -<TD width="163"Bgcolor="#FFFFFF"> Price </td> -</tr> -</HeaderTemplate> in -<ItemTemplate> to<tr style="height:25px;"> +<TD bgcolor="#FFFFFF"><% #Eval ("Name")%></td> -<TD bgcolor="#FFFFFF"><% #Eval (" Time","{0:yyyy mm month DD Day}")%></td> the<TD bgcolor="#FFFFFF"><% #Eval (" Oil")%></td> *<TD bgcolor="#FFFFFF"><% #Eval ("Powers")%></td> $<TD bgcolor="#FFFFFF"><% #Eval (" Price")%></td>Panax Notoginseng</tr> - the</ItemTemplate> + A<FooterTemplate> the +</table> - $</FooterTemplate> $ - -</asp:Repeater> the</form> -</body>
Aspx.cs Code:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingsystem.web;5 usingSystem.Web.UI;6 usingSystem.Web.UI.WebControls;7 8 Public Partial classTest:System.Web.UI.Page9 {Ten protected voidPage_Load (Objectsender, EventArgs e) One { A if(!IsPostBack) - { -Coenctdatacontext _conext =NewCoenctdatacontext (); theRepeater1.datasource=_conext. Car; - Repeater1.databind (); - } - } + - + protected voidSelect_click (Objectsender, EventArgs e) A { atCoenctdatacontext _contest =NewCoenctdatacontext (); - -list<car> list =_contest. Car.tolist (); - stringKey =Gjz.Text.ToString (); - - //determine whether the first condition is empty, if it is empty, to determine whether the second condition is satisfied; not empty, continue to query in if(Key! ="") - { toList = list. Where (p =P.name.contains (key)). ToList (); + //Keyword to change the primary color, use foreach to see the words that meet the criteria in the collection - foreach(Car datainchlist) the { * strings = data. Name.replace (Key,"<span style = ' color:red; ' >"+ key +"</span>"); $Data. Name =s;Panax Notoginseng } - } the + //judging the second condition A stringMi = Price1. Text;//take text to determine if it is empty the stringMa = price2. Text;//take text to determine if it is empty + - //first determine whether the text box is empty in the second condition $ if(Mi! =""&& Ma! ="") $ { - decimalMin =Convert.todecimal (Price1. Text); - decimalMax =Convert.todecimal (Price2. Text); the -list= list. Where (p=>p.price.value>=min && p.price<=max). ToList ();Wuyi the } - Wu //Specify the data source to display -Repeater1.datasource =list; About Repeater1.databind (); $ - } -}
LINQ to SQL multi-conditional query in WebForm (small exercise)