title: Using brackets in dynamic .NET expressions
body: <p>I have a grid in witch a user can fill-in the "filter" on a collection.
  The user has to fill-in some columns:</p>
  
  <pre><code>AndOr  Property  Comparator  Value
  </code></pre>
  
  <p>say, for a Cities collection it could filter cities that</p>
  
  <pre><code>   -  Name       StartsWith  'a'
    AND Population     &gt;       10000
    OR  Population     &lt;       1000
  </code></pre>
  
  <p>I used the dynamic <a href="http://www.albahari.com/nutshell/predicatebuilder.aspx" rel="nofollow">PredicateBuilder</a>, that worked very well, until the "brackets"  requirement appeared.</p>
  
  <p>As you can see from the "query" above, in the resulting collection we will have cities which<br>
  <code>(Name.StartsWith'a' AND Population &gt; 10000) OR (Population &lt; 1000)</code>.</p>
  
  <p>In order to build the expression<br>
   <code>Name.StartsWith'a' AND (Population &gt; 10000 OR (Population &lt; 1000)</code>
  I need to use some brackets.</p>
  
  <p>Now, the filter grid columns changed to</p>
  
  <pre><code>AndOr  LeftBracket  Property  Comparator  Value  RightBracket
  </code></pre>
  
  <p>Is there some "Group", "Open/CloseBracket" in the .NET Dynamic Expression library?
  An other way to realize it?</p>
  
  <p>The code to "link" rows between them was the following</p>
  
  <pre><code>Private Function GetMyObjectsDataSource() As IQueryable(Of MyObject)
    ' start without any filter, get it all '
    Dim predicate = PredicateBuilder.True(Of MyObject)()
    Dim filterExpression As Expression(Of Func(Of MyObject, Boolean)) = predicate
  
    For Each row In grdFilter.Rows
      Dim rowExpression = GetExpressionFromRow(Of MyObject)(row)
      Dim compOp As LogicalOperator = row.Cells(ColumnKeys.AndOr).Value
  
      If compOp = LogicalOperator.Or Then
        filterExpression = [Or](filterExpression, rowExpression)
      Else
        filterExpression = [And](filterExpression, rowExpression)
      End If
    Next row
  
    Dim myObjects As List(Of MyObject) = Me._Container.GetMyObjects()
    Dim result As IQueryable(Of MyObject) =
      myObjects.AsQueryable().Where(filterExpression)
  
    Return result
  End Function
  </code></pre>