title: Can I Return Different Types From An Extension Method
body: <p>I basically want to write a DataTable extension method to hide a generalized LINQ query.  Lets say I have the DataTables below:  </p>
  
  <p><code>DataTable1</code>  </p>
  
  <pre><code>[ID]    [NameOfThing]    [DateOfThing]
  1       'FirstName'      1/2/34
  2       'SecondName'     5/6/78
  </code></pre>
  
  <p><code>DataTable2</code>  </p>
  
  <pre><code>[SomeFieldId]    [NumberOfItems]
  3                934
  4                20393
  </code></pre>
  
  <p>I want to be able to call an extension method like <code>string aString = DataTable1.GetDataItem(1,"ID","NameOfThing")</code><br>
  or<br>
  <code>int anInt = DataTable2.GetDataItem(3,"SomeFieldId","NumberOfItems")</code></p>
  
  <p>I think my extension method would look something like this:  </p>
  
  <pre><code>public static var GetDataItem(this DataTable dTable, int idToMatch
                            ,string fieldToMatchIdTo, string fieldToReturn)  
  {  
      DataRow [] results = dTable.AsEnumerable()  
            .Where(r =&gt; r.Field`&lt;int&gt;`(fieldToMatchTo) == idToMatch).ToArray();  
      return (var)results[0][fieldToReturn];  
  }
  </code></pre>
  
  <p>I know there's a lot missing here (casting to var seems dubious) but hopefully I captured the idea of what I'm trying to do.  Is it possible, or are there any other good alternatives?  I could write an extension method for each return type I suppose but I'm hoping there's a more generalized approach out there.  </p>