title: Abstracting Dynamic Linq to SQL where
body: <p><strong>UPDATE</strong>: Groo's answer was marked correct as it would be a good alternative.  I decided to stick with the select/switch statements to avoid the performance issues of using reflection.  As far as I can tell there is no way to do what I want without using Dynamic LINQ (and reflection) or a decompiler (to manually code each LINQ statement, which I am already doing by hand).  Note: to make this update DropDownList16 through DropDownList20 were removed (needed the characters).</p>
  
  <hr>
  
  <p>Is there a way to abstract the building of a dynamic Linq to SQL query?</p>
  
  <p>I am trying to build a Linq to SQL query with a dynamic where clause, based on user supplied filters.  The user needs to be able to filter strings, integers, and dates using advanced options (equal, not equal, contains, starts with, etc).  The user needs to be able to use these filters with as many or few columns as desired.</p>
  
  <p>My current switch for one column:</p>
  
  <p>VB</p>
  
  <pre><code>Select Case type
      Case StringFilterTypes.Any  ''//Do nothing (same as else)
      Case StringFilterTypes.Contains
          query = From view In query Where view.Name.Contains(userValue) Select view
      Case StringFilterTypes.Exactly
          query = From view In query Where view.Name = userValue Select view
      Case StringFilterTypes.StartsWith
          query = From view In query Where view.Name.StartsWith(userValue) Select view
      Case StringFilterTypes.EndsWith
          query = From view In query Where view.Name.EndsWith(userValue) Select view
      Case Else                   ''//Do nothing (same as Any).
  End Select
  </code></pre>
  
  <p>C#</p>
  
  <pre><code>switch (type) {
      case StringFilterTypes.Any: //Do nothing (same as else)
          break;
      case StringFilterTypes.Contains:
          query = from view in querywhere view.Name.Contains(userValue)view;
          break;
      case StringFilterTypes.Exactly:
          query = from view in querywhere view.Name == userValueview;
          break;
      case StringFilterTypes.StartsWith:
          query = from view in querywhere view.Name.StartsWith(userValue)view;
          break;
      case StringFilterTypes.EndsWith:
          query = from view in querywhere view.Name.EndsWith(userValue)view;
          break;
      default:                    //Do nothing (same as Any).
          break;
  }
  </code></pre>
  
  <p>I will need to use this select/switch with all the string columns and a similar switch with all the integer and datetime columns.  As a result, I want to make these select/switch statments into a function where the type, view.Name, and userValue are supplied variables.  I can pass type and userValue since they are local variables, but how can I pass view.Name into the function?</p>
  
  <p>I have looked into extensions, but they seem to require that I pass view, know the column already, and don't support select/switch statements.<br>
  I also looked into <a href="http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx" rel="nofollow">adding the where statements as strings</a> but feel this removes the only two reasons for using linq (compiler verified queries, automated escaping of user input to prevent sql injection).<br>
  <a href="http://msdn.microsoft.com/en-us/library/bb397951.aspx" rel="nofollow">Expression Trees</a> look promising, but I am not sure how to use them and if they are a good idea where performance is an issue.</p>
  
  <p>The best solution would be something like:</p>
  
  <p>VB</p>
  
  <pre><code>Public Function ApplyFilters(query As IQueryable(Of DBName.ViewName)) As IQueryable(Of DBName.ViewName)
      ''//...
      Filter_String (query, Type, View.Name, userValue)
      ''//...
  End Function
  Public Function Filter_String (query As IQueryable(Of DBName.ViewName), type As StringFilterTypes, column as ???, userValue As String) As IQueryable(Of DBName.ViewName)
      Select Case type
          Case StringFilterTypes.Any  ''//Do nothing (same as else)
          Case StringFilterTypes.Contains
              query = From view In query Where column.Contains(userValue) Select view
          Case StringFilterTypes.Exactly
              query = From view In query Where column = userValue Select view
          Case StringFilterTypes.StartsWith
              query = From view In query Where column.StartsWith(userValue) Select view
          Case StringFilterTypes.EndsWith
              query = From view In query Where column.EndsWith(userValue) Select view
          Case Else                   ''//Do nothing (same as Any).
      End Select
  
      Return query
  End Function
  </code></pre>
  
  <p>C#</p>
  
  <pre><code>public IQueryable&lt;DBName.ViewName&gt; ApplyFilters(IQueryable&lt;DBName.ViewName&gt; query)
  {
      //...
      Filter_String (query, Type, View.Name, userValue);
      //...
  }
  public IQueryable&lt;DBName.ViewName&gt; Filter_String(IQueryable&lt;DBName.ViewName&gt; query, StringFilterTypes type, ??? column, string userValue)
  {
      switch (type) {
          case StringFilterTypes.Any: //Do nothing (same as else)
              break;
          case StringFilterTypes.Contains:
              query = from view in querywhere column.Contains(userValue)view;
              break;
          case StringFilterTypes.Exactly:
              query = from view in querywhere column == userValueview;
              break;
          case StringFilterTypes.StartsWith:
              query = from view in querywhere column.StartsWith(userValue)view;
              break;
          case StringFilterTypes.EndsWith:
              query = from view in querywhere column.EndsWith(userValue)view;
              break;
          default:
              break;                  //Do nothing (same as Any).
      }
  
      return query;
  }
  </code></pre>
  
  <p><strong>Update 2012/02/27:</strong><br>
  For BlueRaja - Danny Pflughoeft I am providing a sample application.  Obviously the data connection will have to be supplied by the end user (MS SQL database named "Test" with table named "testTable" and columns named "Column#" where # is all numbers 1-20).</p>
  
  <p>Default.aspx</p>
  
  <pre><code>&lt;%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication1.test._Default" %&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
          &lt;asp:DropDownList ID="DropDownList1" runat="server"&gt;
              &lt;asp:ListItem Selected="True"  Text ="Any"          Value="-1" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Contains"     Value="0" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Is exactly"   Value="1" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Starts with"  Value="2" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Ends with"    Value="3" /&gt;
          &lt;/asp:DropDownList&gt;
          &lt;asp:TextBox ID="TextBox1" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;br /&gt;
  
          &lt;asp:DropDownList ID="DropDownList2" runat="server"&gt;
              &lt;asp:ListItem Selected="True"  Text ="Any"          Value="-1" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Contains"     Value="0" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Is exactly"   Value="1" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Starts with"  Value="2" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Ends with"    Value="3" /&gt;
          &lt;/asp:DropDownList&gt;
          &lt;asp:TextBox ID="TextBox2" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;br /&gt;
  
          &lt;asp:DropDownList ID="DropDownList3" runat="server"&gt;
              &lt;asp:ListItem Selected="True"  Text ="Any"          Value="-1" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Contains"     Value="0" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Is exactly"   Value="1" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Starts with"  Value="2" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Ends with"    Value="3" /&gt;
          &lt;/asp:DropDownList&gt;
          &lt;asp:TextBox ID="TextBox3" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;br /&gt;
  
          &lt;asp:DropDownList ID="DropDownList4" runat="server"&gt;
              &lt;asp:ListItem Selected="True"  Text ="Any"          Value="-1" /&gt;
              &lt;asp:ListItem Selected="False"  Text ="Contains"    Value="0" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Is exactly"   Value="1" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Starts with"  Value="2" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Ends with"    Value="3" /&gt;
          &lt;/asp:DropDownList&gt;
          &lt;asp:TextBox ID="TextBox4" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;br /&gt;
  
          &lt;asp:DropDownList ID="DropDownList5" runat="server"&gt;
              &lt;asp:ListItem Selected="True"  Text ="Any"          Value="-1" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Contains"     Value="0" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Is exactly"   Value="1" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Starts with"  Value="2" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Ends with"    Value="3" /&gt;
          &lt;/asp:DropDownList&gt;
          &lt;asp:TextBox ID="TextBox5" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;br /&gt;
  
          &lt;asp:DropDownList ID="DropDownList6" runat="server"&gt;
              &lt;asp:ListItem Selected="True"  Text ="Any"          Value="-1" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Contains"     Value="0" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Is exactly"   Value="1" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Starts with"  Value="2" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Ends with"    Value="3" /&gt;
          &lt;/asp:DropDownList&gt;
          &lt;asp:TextBox ID="TextBox6" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;br /&gt;
  
          &lt;asp:DropDownList ID="DropDownList7" runat="server"&gt;
              &lt;asp:ListItem Selected="True"  Text ="Any"          Value="-1" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Contains"     Value="0" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Is exactly"   Value="1" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Starts with"  Value="2" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Ends with"    Value="3" /&gt;
          &lt;/asp:DropDownList&gt;
          &lt;asp:TextBox ID="TextBox7" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;br /&gt;
  
          &lt;asp:DropDownList ID="DropDownList8" runat="server"&gt;
              &lt;asp:ListItem Selected="True"  Text ="Any"          Value="-1" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Contains"     Value="0" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Is exactly"   Value="1" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Starts with"  Value="2" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Ends with"    Value="3" /&gt;
          &lt;/asp:DropDownList&gt;
          &lt;asp:TextBox ID="TextBox8" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;br /&gt;
  
          &lt;asp:DropDownList ID="DropDownList9" runat="server"&gt;
              &lt;asp:ListItem Selected="True"  Text ="Any"          Value="-1" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Contains"     Value="0" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Is exactly"   Value="1" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Starts with"  Value="2" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Ends with"    Value="3" /&gt;
          &lt;/asp:DropDownList&gt;
          &lt;asp:TextBox ID="TextBox9" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;br /&gt;
  
          &lt;asp:DropDownList ID="DropDownList10" runat="server"&gt;
              &lt;asp:ListItem Selected="True"  Text ="Any"          Value="-1" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Contains"     Value="0" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Is exactly"   Value="1" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Starts with"  Value="2" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Ends with"    Value="3" /&gt;
          &lt;/asp:DropDownList&gt;
          &lt;asp:TextBox ID="TextBox10" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;br /&gt;
  
          &lt;asp:DropDownList ID="DropDownList11" runat="server"&gt;
              &lt;asp:ListItem Selected="True"  Text ="Any"          Value="-1" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Contains"     Value="0" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Is exactly"   Value="1" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Starts with"  Value="2" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Ends with"    Value="3" /&gt;
          &lt;/asp:DropDownList&gt;
          &lt;asp:TextBox ID="TextBox11" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;br /&gt;
  
          &lt;asp:DropDownList ID="DropDownList12" runat="server"&gt;
              &lt;asp:ListItem Selected="True"  Text ="Any"          Value="-1" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Contains"     Value="0" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Is exactly"   Value="1" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Starts with"  Value="2" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Ends with"    Value="3" /&gt;
          &lt;/asp:DropDownList&gt;
          &lt;asp:TextBox ID="TextBox12" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;br /&gt;
  
          &lt;asp:DropDownList ID="DropDownList13" runat="server"&gt;
              &lt;asp:ListItem Selected="True"  Text ="Any"          Value="-1" /&gt;
              &lt;asp:ListItem Selected="False"  Text ="Contains"    Value="0" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Is exactly"   Value="1" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Starts with"  Value="2" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Ends with"    Value="3" /&gt;
          &lt;/asp:DropDownList&gt;
          &lt;asp:TextBox ID="TextBox13" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;br /&gt;
  
          &lt;asp:DropDownList ID="DropDownList14" runat="server"&gt;
              &lt;asp:ListItem Selected="True"  Text ="Any"          Value="-1" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Contains"     Value="0" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Is exactly"   Value="1" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Starts with"  Value="2" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Ends with"    Value="3" /&gt;
          &lt;/asp:DropDownList&gt;
          &lt;asp:TextBox ID="TextBox14" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;br /&gt;
  
          &lt;asp:DropDownList ID="DropDownList15" runat="server"&gt;
              &lt;asp:ListItem Selected="True"  Text ="Any"          Value="-1" /&gt;
              &lt;asp:ListItem Selected="False"  Text ="Contains"    Value="0" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Is exactly"   Value="1" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Starts with"  Value="2" /&gt;
              &lt;asp:ListItem Selected="False" Text ="Ends with"    Value="3" /&gt;
          &lt;/asp:DropDownList&gt;
          &lt;asp:TextBox ID="TextBox15" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;br /&gt;
  
          &lt;asp:GridView ID="GridView1" runat="server"&gt;
          &lt;/asp:GridView&gt;
  
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Default.aspx.vb</p>
  
  <pre><code>Public Class _Default
      Inherits System.Web.UI.Page
  
      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  
      End Sub
  
      Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
          Dim _db As TestDataContext = New TestDataContext(ConfigurationManager.ConnectionStrings("TestDataConn").ConnectionString)
  
          Dim query As IQueryable(Of testTable) = _
           From view In _db.testTables
           Select view
  
          Select Case DropDownList1.SelectedValue
              Case "-1"
                  ''//Do nothing (same as else)
              Case "0"
                  query = From view In query Where view.Column1.Contains(TextBox1.Text) Select view
              Case "1"
                  query = From view In query Where view.Column1 = TextBox1.Text Select view
              Case "2"
                  query = From view In query Where view.Column1.StartsWith(TextBox1.Text) Select view
              Case "3"
                  query = From view In query Where view.Column1.EndsWith(TextBox1.Text) Select view
              Case Else
                  ''//Do nothing (same as Any).
          End Select
  
          Select Case DropDownList2.SelectedValue
              Case "-1"
                  ''//Do nothing (same as else)
              Case "0"
                  query = From view In query Where view.Column2.Contains(TextBox2.Text) Select view
              Case "1"
                  query = From view In query Where view.Column2 = TextBox2.Text Select view
              Case "2"
                  query = From view In query Where view.Column2.StartsWith(TextBox2.Text) Select view
              Case "3"
                  query = From view In query Where view.Column2.EndsWith(TextBox2.Text) Select view
              Case Else
                  ''//Do nothing (same as Any).
          End Select
  
          Select Case DropDownList3.SelectedValue
              Case "-1"
                  ''//Do nothing (same as else)
              Case "0"
                  query = From view In query Where view.Column3.Contains(TextBox3.Text) Select view
              Case "1"
                  query = From view In query Where view.Column3 = TextBox3.Text Select view
              Case "2"
                  query = From view In query Where view.Column3.StartsWith(TextBox3.Text) Select view
              Case "3"
                  query = From view In query Where view.Column3.EndsWith(TextBox3.Text) Select view
              Case Else
                  ''//Do nothing (same as Any).
          End Select
  
          Select Case DropDownList4.SelectedValue
              Case "-1"
                  ''//Do nothing (same as else)
              Case "0"
                  query = From view In query Where view.Column4.Contains(TextBox4.Text) Select view
              Case "1"
                  query = From view In query Where view.Column4 = TextBox4.Text Select view
              Case "2"
                  query = From view In query Where view.Column4.StartsWith(TextBox4.Text) Select view
              Case "3"
                  query = From view In query Where view.Column4.EndsWith(TextBox4.Text) Select view
              Case Else
                  ''//Do nothing (same as Any).
          End Select
  
          Select Case DropDownList5.SelectedValue
              Case "-1"
                  ''//Do nothing (same as else)
              Case "0"
                  query = From view In query Where view.Column5.Contains(TextBox5.Text) Select view
              Case "1"
                  query = From view In query Where view.Column5 = TextBox5.Text Select view
              Case "2"
                  query = From view In query Where view.Column5.StartsWith(TextBox5.Text) Select view
              Case "3"
                  query = From view In query Where view.Column5.EndsWith(TextBox5.Text) Select view
              Case Else
                  ''//Do nothing (same as Any).
          End Select
  
          Select Case DropDownList6.SelectedValue
              Case "-1"
                  ''//Do nothing (same as else)
              Case "0"
                  query = From view In query Where view.Column6.Contains(TextBox6.Text) Select view
              Case "1"
                  query = From view In query Where view.Column6 = TextBox6.Text Select view
              Case "2"
                  query = From view In query Where view.Column6.StartsWith(TextBox6.Text) Select view
              Case "3"
                  query = From view In query Where view.Column6.EndsWith(TextBox6.Text) Select view
              Case Else
                  ''//Do nothing (same as Any).
          End Select
  
          Select Case DropDownList7.SelectedValue
              Case "-1"
                  ''//Do nothing (same as else)
              Case "0"
                  query = From view In query Where view.Column7.Contains(TextBox7.Text) Select view
              Case "1"
                  query = From view In query Where view.Column7 = TextBox7.Text Select view
              Case "2"
                  query = From view In query Where view.Column7.StartsWith(TextBox7.Text) Select view
              Case "3"
                  query = From view In query Where view.Column7.EndsWith(TextBox7.Text) Select view
              Case Else
                  ''//Do nothing (same as Any).
          End Select
  
          Select Case DropDownList8.SelectedValue
              Case "-1"
                  ''//Do nothing (same as else)
              Case "0"
                  query = From view In query Where view.Column8.Contains(TextBox8.Text) Select view
              Case "1"
                  query = From view In query Where view.Column8 = TextBox8.Text Select view
              Case "2"
                  query = From view In query Where view.Column8.StartsWith(TextBox8.Text) Select view
              Case "3"
                  query = From view In query Where view.Column8.EndsWith(TextBox8.Text) Select view
              Case Else
                  ''//Do nothing (same as Any).
          End Select
  
          Select Case DropDownList9.SelectedValue
              Case "-1"
                  ''//Do nothing (same as else)
              Case "0"
                  query = From view In query Where view.Column9.Contains(TextBox9.Text) Select view
              Case "1"
                  query = From view In query Where view.Column9 = TextBox9.Text Select view
              Case "2"
                  query = From view In query Where view.Column9.StartsWith(TextBox9.Text) Select view
              Case "3"
                  query = From view In query Where view.Column9.EndsWith(TextBox9.Text) Select view
              Case Else
                  ''//Do nothing (same as Any).
          End Select
  
          Select Case DropDownList10.SelectedValue
              Case "-1"
                  ''//Do nothing (same as else)
              Case "0"
                  query = From view In query Where view.Column10.Contains(TextBox10.Text) Select view
              Case "1Column10"
                  query = From view In query Where view.Column10 = TextBox10.Text Select view
              Case "2"
                  query = From view In query Where view.Column10.StartsWith(TextBox10.Text) Select view
              Case "3"
                  query = From view In query Where view.Column10.EndsWith(TextBox10.Text) Select view
              Case Else
                  ''//Do nothing (same as Any).
          End Select
  
          Select Case DropDownList11.SelectedValue
              Case "-1"
                  ''//Do nothing (same as else)
              Case "0"
                  query = From view In query Where view.Column11.Contains(TextBox11.Text) Select view
              Case "1"
                  query = From view In query Where view.Column11 = TextBox11.Text Select view
              Case "2"
                  query = From view In query Where view.Column11.StartsWith(TextBox11.Text) Select view
              Case "3"
                  query = From view In query Where view.Column11.EndsWith(TextBox11.Text) Select view
              Case Else
                  ''//Do nothing (same as Any).
          End Select
  
          Select Case DropDownList12.SelectedValue
              Case "-1"
                  ''//Do nothing (same as else)
              Case "0"
                  query = From view In query Where view.Column12.Contains(TextBox12.Text) Select view
              Case "1"
                  query = From view In query Where view.Column12 = TextBox12.Text Select view
              Case "2"
                  query = From view In query Where view.Column12.StartsWith(TextBox12.Text) Select view
              Case "3"
                  query = From view In query Where view.Column12.EndsWith(TextBox12.Text) Select view
              Case Else
                  ''//Do nothing (same as Any).
          End Select
  
          Select Case DropDownList13.SelectedValue
              Case "-1"
                  ''//Do nothing (same as else)
              Case "0"
                  query = From view In query Where view.Column13.Contains(TextBox13.Text) Select view
              Case "1"
                  query = From view In query Where view.Column13 = TextBox13.Text Select view
              Case "2"
                  query = From view In query Where view.Column13.StartsWith(TextBox13.Text) Select view
              Case "3"
                  query = From view In query Where view.Column13.EndsWith(TextBox13.Text) Select view
              Case Else
                  ''//Do nothing (same as Any).
          End Select
  
          Select Case DropDownList14.SelectedValue
              Case "-1"
                  ''//Do nothing (same as else)
              Case "0"
                  query = From view In query Where view.Column14.Contains(TextBox14.Text) Select view
              Case "1"
                  query = From view In query Where view.Column14 = TextBox14.Text Select view
              Case "2"
                  query = From view In query Where view.Column14.StartsWith(TextBox14.Text) Select view
              Case "3"
                  query = From view In query Where view.Column14.EndsWith(TextBox14.Text) Select view
              Case Else
                  ''//Do nothing (same as Any).
          End Select
  
          Select Case DropDownList15.SelectedValue
              Case "-1"
                  ''//Do nothing (same as else)
              Case "0"
                  query = From view In query Where view.Column15.Contains(TextBox15.Text) Select view
              Case "1"
                  query = From view In query Where view.Column15 = TextBox15.Text Select view
              Case "2"
                  query = From view In query Where view.Column15.StartsWith(TextBox15.Text) Select view
              Case "3"
                  query = From view In query Where view.Column15.EndsWith(TextBox15.Text) Select view
              Case Else
                  ''//Do nothing (same as Any).
          End Select
  
  
          GridView1.DataSource = query
          GridView1.DataBind()
      End Sub
  
  End Class
  </code></pre>