Data Access has been discontinued. Please refer to this page for more information.

Filtering Data

The OpenAccessLinqDataSource control provides a number of services that help you add advanced capabilities to your applications. This includes filtering data. This topic illustrates how to use the OpenAccessLinqDataSource control to filter the database records:

Filtering Which Records to Retrieve

The Where property of OpenAccessLinqDataSource allows you to filter which records to be returned. If you do not specify a value for the Where property, the OpenAccessLinqDataSource control returns all the records from the database table.

To specify a filtering clause in the datasource, you need to define a value for the Where property that can be evaluated to either True or *False, and to create a parameter that will carry the condition.
The following example shows the markup for part of an ASP.NET Web page that contains an *
OpenAccessLinqDataSource*. The control is configured to enable the user to display all columns from a table named *Categories. The returned records are filtered by the CategoryName column. Only records which CategoryName is equal to SUV will be returned.

<telerik:OpenAccessLinqDataSource
    ID="OpenAccessLinqDataSourceCategory"
    runat="server" EntityTypeName=""
    ContextTypeName="SofiaCarRental.Model.FluentModel"
    ResourceSetName="Categories" Where="CategoryName == @CategoryName">
    <WhereParameters>
        <asp:Parameter DefaultValue="SUV" Name="CategoryName" Type="String">
        </asp:Parameter>
    </WhereParameters>
</telerik:OpenAccessLinqDataSource>

How to: Change the Filter Expression Runtime

To change the filter expression runtime, you need to attach to the Selecting event. The Selecting event occurs before a data-retrieval operation. The OpenAccessLinqDataSourceSelectEventArgs object that is passed to the event handler contains the parameters for the data-retrieval operation. You can modify the parameters in the Selecting event handler before the query executes.

protected void OpenAccessLinqDataSourceCategory_Selecting(object sender, 
    Telerik.OpenAccess.Web.OpenAccessLinqDataSourceSelectEventArgs e)
{
   if (e.WhereParameters.ContainsKey("CategoryName"))
   {
       e.WhereParameters["CategoryName"] = "Saloon";
   }
}
Protected Sub OpenAccessLinqDataSourceCategory_Selecting(ByVal sender As Object, _
    ByVal e As Telerik.OpenAccess.Web.OpenAccessLinqDataSourceSelectEventArgs) _
    Handles OpenAccessLinqDataSource1.Selecting
 If e.WhereParameters.ContainsKey("CategoryName") Then
  e.WhereParameters("CategoryName") = "Saloon"
 End If
End Sub

Parameter Sources

You could obtain values for the specified parameter from other sources:

  • None - the Default Value will be used as the parameter value.

    <telerik:OpenAccessLinqDataSource
        ID="OpenAccessLinqDataSourceCategory"
        runat="server" EntityTypeName=""
        ContextTypeName="SofiaCarRental.Model.FluentModel"
        ResourceSetName="Categories" Where="CategoryName == @CategoryName">
        <WhereParameters>
            <asp:Parameter DefaultValue="SUV" Name="CategoryName" Type="String">
            </asp:Parameter>
        </WhereParameters>
    </telerik:OpenAccessLinqDataSource>
    
  • Cookie - sets a parameter to the value of an HttpCookie object. You specify the name of the HttpCookie object using the Cookie name property. If the specified cookie object does not exist, then the Default Value is used as the parameter value.

    <telerik:OpenAccessLinqDataSource
        ID="OpenAccessLinqDataSourceCategory"
        runat="server" EntityTypeName=""
        ContextTypeName="SofiaCarRental.Model.FluentModel"
        ResourceSetName="Categories" Where="CategoryName == @CategoryName">
        <WhereParameters>
            <asp:CookieParameter CookieName="MyWebAppCoockie" DefaultValue="SUV" 
                Name="CategoryName" Type="String">
            </asp:CookieParameter>
        </WhereParameters>
    </telerik:OpenAccessLinqDataSource>
    
  • Control - sets a parameter to the property value of a control on an ASP.NET Web page. You specify the control by using the Control ID property. Some controls that derive from the Control class define a ControlValuePropertyAttribute, which determines the default property from which to retrieve the control's value. For example, RadComboBox.SelectedValue.

    <telerik:OpenAccessLinqDataSource
        ID="OpenAccessLinqDataSourceCategory"
        runat="server" EntityTypeName=""
        ContextTypeName="SofiaCarRental.Model.FluentModel"
        ResourceSetName="Categories" Where="CategoryName == @CategoryName">
        <WhereParameters>
            <asp:ControlParameter ControlID="RadComboBox1" PropertyName="SelectedValue" 
                DefaultValue="SUV" Name="CategoryName" Type="String">
            </asp:ControlParameter>
        </WhereParameters>
    </telerik:OpenAccessLinqDataSource>
    
  • Form - sets a parameter to the value of an HTML form field. You specify the name of the HTML form field by using the Form field setting. If the specified HTML form field value does not exist, then the Default Value is used as the parameter value.

    <telerik:OpenAccessLinqDataSource
        ID="OpenAccessLinqDataSourceCategory"
        runat="server" EntityTypeName=""
        ContextTypeName="SofiaCarRental.Model.FluentModel"
        ResourceSetName="Categories" Where="CategoryName == @CategoryName">
        <WhereParameters>
            <asp:FormParameter FormField="Name" DefaultValue="SUV" Name="CategoryName" Type="String">
            </asp:FormParameter>
        </WhereParameters>
    </telerik:OpenAccessLinqDataSource>
    
  • Profile - sets a parameter to the value of a property from the current user profile. You specify the name of the profile property by using the Property name setting. If the specified profile property does not exist, then the Default Value is used as the parameter value.

    <telerik:OpenAccessLinqDataSource
        ID="OpenAccessLinqDataSourceCategory"
        runat="server" EntityTypeName=""
        ContextTypeName="SofiaCarRental.Model.FluentModel"
        ResourceSetName="Categories" Where="CategoryName == @CategoryName">
        <WhereParameters>
            <asp:ProfileParameter PropertyName="CarCategory" DefaultValue="SUV" 
                Name="CategoryName" Type="String">
            </asp:ProfileParameter>
        </WhereParameters>
    </telerik:OpenAccessLinqDataSource>
    
  • QueryString - sets a parameter to the value of a QueryString field. You specify the name of the QueryString field by using the Property name setting. If the specified QueryString property does not exist, then the Default Value is used as the parameter value.

    <telerik:OpenAccessLinqDataSource
        ID="OpenAccessLinqDataSourceCategory"
        runat="server" EntityTypeName=""
        ContextTypeName="SofiaCarRental.Model.FluentModel"
        ResourceSetName="Categories" Where="CategoryName == @CategoryName">
        <WhereParameters>
            <asp:QueryStringParameter QueryStringField="catname" DefaultValue="SUV" 
                Name="CategoryName" Type="String">
            </asp:QueryStringParameter>
        </WhereParameters>
    </telerik:OpenAccessLinqDataSource>
    
  • Session - sets a parameter to the value of a Session object. You specify the name of the Session object by using the Session field setting. If the specified Session object does not exist, then the Default Value is used as the parameter value.

    <telerik:OpenAccessLinqDataSource
        ID="OpenAccessLinqDataSourceCategory"
        runat="server" EntityTypeName=""
        ContextTypeName="SofiaCarRental.Model.FluentModel"
        ResourceSetName="Categories" Where="CategoryName == @CategoryName">
        <WhereParameters>
            <asp:SessionParameter SessionField="CategoryName" DefaultValue="SUV" 
                Name="CategoryName" Type="String">
            </asp:SessionParameter>
        </WhereParameters>
    </telerik:OpenAccessLinqDataSource>
    
  • RouteData - uses routes to obtain the parameter value. If the specified Route key does not exist, then the Default Value is used as the parameter value.

    <telerik:OpenAccessLinqDataSource
        ID="OpenAccessLinqDataSourceCategory"
        runat="server" EntityTypeName=""
        ContextTypeName="SofiaCarRental.Model.FluentModel"
        ResourceSetName="Categories" Where="CategoryName == @CategoryName">
        <WhereParameters>
            <asp:RouteParameter RouteKey="catname" DefaultValue="SUV" 
                Name="CategoryName" Type="String">
            </asp:RouteParameter>
        </WhereParameters>
    </telerik:OpenAccessLinqDataSource>