title: Create a dynamic SOQL Query using variable objects
body: <p>I'm trying to execute a dynamic soql query using variable objects.
  In my visualforce page i have tow apex:selectlist, the first one contains a list of objects, when i select one object from this list, i refresh the second list to display selected object's fields.
  The apex:inputText contains text to search in selected field.</p>
  
  <h2>visual force code:</h2>
  
  <pre><code>&lt;apex:selectList id="listObjects" value="{!selectedObject}" size="1"&gt;
      &lt;apex:selectOptions value="{!allObjetcs}"&gt;&lt;/apex:selectOptions&gt;
  &lt;/apex:SelectList&gt; 
  &lt;apex:selectList id="listFields" value="{!selectedField}" size="1"&gt;
      &lt;apex:selectOptions value="{!allFields}"&gt;&lt;/apex:selectOptions&gt;
  &lt;/apex:SelectList&gt; 
  &lt;label&gt;Text to search : &lt;/label&gt;&lt;apex:inputText id="textResearch" value="{!textResearch}" /&gt; 
  &lt;button id="searchButton" type="button"&gt;{!$Label.SEARCH}&lt;/button&gt; 
  </code></pre>
  
  <h2>apex code :</h2>
  
  <pre><code>public void search() {
          result = new List&lt;SearchWrapper&gt;();
          System.debug('&gt;&gt;&gt;&gt;&gt;&gt; ALK - in search ');
          String query = 'Select Id, ' + selectedField + ' from ' + selectedObject + ' where ' + selectedField + ' like \'%' + textResearch + '\'%';
          System.debug('&gt;&gt;&gt;&gt;&gt;&gt; ALK - Query : ' + query);
          List&lt;sObject&gt; = (sObject) Database.query(query);
  
  }
  </code></pre>
  
  <p>Please how can i cast Database.query(query) and how can i execute this dynamic query.</p>
  
  <p>Thanks for all.</p>