Click here to Skip to main content
15,895,746 members
Articles / Programming Languages / C#

LINQ and Dynamic Predicate Construction at Runtime

Rate me:
Please Sign up or sign in to vote.
4.88/5 (29 votes)
13 Aug 2008CPOL7 min read 207.4K   2.2K   87  
Illustrating a multi-predicate injection pattern now possible with the new features of C# 3.0.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="LINQDynamicSearchDemo._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <h1>Employee Impromptu Search Tool</h1>
        <p>Check the boxes to use each filter. Each text field supports partial matches, and the Birth Date is to be an inclusive date range.</p>
        <table>
            <tr>
                <td><asp:CheckBox ID="cbxUseEmployeeID" runat="server" Checked="false" 
                        Text="Use ID" /></td>
                <td><asp:TextBox ID="filterEmployeeId" runat="server"/></td>
                <td>&nbsp;</td>
                <td><asp:CheckBox ID="cbxUseLastName" runat="server" Checked="false" 
                        Text="Use Last Name" /></td>
                <td><asp:TextBox ID="filterLastName" runat="server" /></td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td><asp:CheckBox ID="cbxUseFirstName" runat="server" Checked="false" 
                        Text="Use First Name" /></td>
                <td><asp:TextBox ID="filterFirstName" runat="server" /></td>
                <td>&nbsp;</td>
                <td><asp:CheckBox ID="cbxUseTitle" runat="server" Checked="false" 
                        Text="Use Title" /></td>
                <td><asp:TextBox ID="filterTitle" runat="server" /></td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td><asp:CheckBox ID="cbxUseBirthDate" runat="server" Checked="false" 
                        Text="Birth Date Range" /></td>
                <td><asp:TextBox ID="filterBirthDateStart" runat="server" /></td>
                <td><asp:TextBox ID="filterBirthDateEnd" runat="server" /></td>
                <td><asp:CheckBox ID="cbxUseAddress" runat="server" Checked="false" 
                        Text="Use Address" /></td>
                <td><asp:TextBox ID="filterAddress" runat="server" /></td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td><asp:CheckBox ID="cbxUseCity" runat="server" Checked="false" Text="Use City" /></td>
                <td><asp:TextBox ID="filterCity" runat="server" /></td>
                <td>&nbsp;</td>
                <td><asp:CheckBox ID="cbxUseState" runat="server" Checked="false" 
                        Text="Use State" /></td>
                <td><asp:DropDownList ID="filterState" DataSourceID="StateSource" DataTextField="Region" DataValueField="Region" runat="server" /></td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td><asp:CheckBox ID="cbxUsePostalCode" runat="server" Checked="false" 
                        Text="Use Post Code" /></td>
                <td><asp:TextBox ID="filterPostalCode" runat="server" /></td>
                <td>&nbsp;</td>
                <td><asp:CheckBox ID="cbxUseCountry" runat="server" Checked="false" 
                        Text="Use Country" /></td>
                <td><asp:DropDownList ID="filterCountry" runat="server" DataSourceID="CountrySource" DataTextField="Country" DataValueField="Country" /></td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td><asp:CheckBox ID="cbxUseHomePhone" runat="server" Checked="false" 
                        Text="Use Phone" /></td>
                <td><asp:TextBox ID="filterHomePhone" runat="server" /></td>
                <td>&nbsp;</td>
                <td><asp:CheckBox ID="cbxUseNotes" runat="server" Checked="false" 
                        Text="Use Notes" /></td>
                <td><asp:TextBox ID="filterNotes" runat="server" /></td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td><asp:Button ID="searchButton" runat="server" Text="Search" 
                        onclick="searchButton_Click" /></td>
                <td colspan="5" />
            </tr>
        </table>
        
        <asp:GridView ID="ResultGrid" runat="server" AutoGenerateColumns="False" 
            AllowPaging="True" BackColor="White" BorderColor="#999999" BorderStyle="None" 
            BorderWidth="1px" CellPadding="3" GridLines="Vertical" 
            onpageindexchanging="ResultGrid_PageIndexChanging">
            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
            <Columns>
                <asp:BoundField DataField="EmployeeID" HeaderText="ID" 
                    SortExpression="EmployeeID" />
                <asp:BoundField DataField="LastName" HeaderText="Last Name" 
                    SortExpression="LastName" />
                <asp:BoundField DataField="FirstName" HeaderText="First Name" 
                    SortExpression="FirstName" />
                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                <asp:BoundField DataField="TitleOfCourtesy" HeaderText="TitleOfCourtesy" 
                    SortExpression="TitleOfCourtesy" visible="false"/>
                <asp:BoundField DataField="BirthDate" HeaderText="DOB" 
                    SortExpression="BirthDate" DataFormatString="{0:d}" />
                <asp:BoundField DataField="HireDate" HeaderText="Hired" 
                    SortExpression="HireDate" visible="false"/>
                <asp:BoundField DataField="Address" HeaderText="Address" 
                    SortExpression="Address" />
                <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
                <asp:BoundField DataField="Region" HeaderText="State" 
                    SortExpression="Region" />
                <asp:BoundField DataField="PostalCode" HeaderText="Postal Code" 
                    SortExpression="PostalCode" />
                <asp:BoundField DataField="Country" HeaderText="Country" 
                    SortExpression="Country" />
                <asp:BoundField DataField="HomePhone" HeaderText="Phone" 
                    SortExpression="HomePhone" />
                <asp:BoundField DataField="Extension" HeaderText="Extension" 
                    SortExpression="Extension" Visible="false" />
                <asp:BoundField DataField="Notes" HeaderText="Notes" SortExpression="Notes" />
                <asp:BoundField DataField="ReportsTo" HeaderText="Boss" 
                    SortExpression="ReportsTo" Visible="false" />
                <asp:BoundField DataField="PhotoPath" HeaderText="Photo Url" 
                    SortExpression="PhotoPath" Visible="false" />
            </Columns>
            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="#DCDCDC" />
        </asp:GridView>
        
    </div>
    <asp:SqlDataSource ID="CountrySource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
        SelectCommand="SELECT DISTINCT [Country] FROM [Employees]">
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="StateSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
        SelectCommand="SELECT DISTINCT [Region] FROM [Employees]"></asp:SqlDataSource>
    </form>
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Dave works all day, and stays up all night coding and reading, surfing the intertubes.

Comments and Discussions