title: Extension Select methods for Entity Framework
body: <p>I have many similar calls to database and they work fine</p>
  
  <pre><code>Using ta As New QT_SSTEntities
   Return ta.Product.
     Select(Function(c) New RadComboBoxData() With {.Text = c.FieldName1, .Value = c.FieldName1}).
     ToArray()
  End Using
  
  Using ta As New QT_SSTEntities
   Return ta.City.
     Select(Function(c) New RadComboBoxData() With {.Text = c.CityName, .Value = c.CityName}).
     ToArray()
  End Using
  
  Using ta As New QT_SSTEntities
   Return ta.OtherProduct.
     Select(Function(c) New RadComboBoxData() With {.Text = c.FieldName3, .Value = c.FieldName3}).
     ToArray()
  End Using
  </code></pre>
  
  <p>They produce sql code like that:</p>
  
  <pre><code>  SELECT FieldName1 FROM Product
  </code></pre>
  
  <p>I want to extact </p>
  
  <pre><code>Select(Function(c) New RadComboBoxData() With {.Text = c.SomeField, .Value = c.SomeField}).ToArray()
  </code></pre>
  
  <p>into another method (smth. like that but it doesn't work).</p>
  
  <pre><code>&lt;Extension()&gt;
  Public Function ToRadComboBoxData(Of TSource, TItem)(ByVal source As IQueryable(Of TSource), selector As Expression(of Func(Of TSource, TItem))) As RadComboBoxData()
     Dim r = (From row In source Select selector(row)).ToArray() 'Build tree from expression. How???
     Return (From v In r
       Let rv = If(v Is Nothing, Nothing, v.ToString())
       Select New RadComboBoxData() With {.Text = rv, .Value = rv}).ToArray()
  
  End Function
  </code></pre>
  
  <p>and make calls in the following way:</p>
  
  <pre><code>Using ta As New QT_SSTEntities
    Return ta.Product.ToRadComboBoxData(Function(c) c.FieldName1)
  End Using
  
  Using ta As New QT_SSTEntities
    Return ta.City.ToRadComboBoxData(Function(c) c.CityName)
  End Using
  
  Using ta As New QT_SSTEntities
    Return ta.OtherProduct.ToRadComboBoxData(Function(c) c.FieldName3)
  End Using
  </code></pre>