Note that there are some explanatory texts on larger screens.

plurals
  1. PO"Selecting" or "Wrapping" an IQueryable so that it is still queryable
    primarykey
    data
    text
    <p>I have a Class / API that uses an <code>IQueryable&lt;FirstClass&gt;</code> data source however I wish to expose an <code>IQueryable&lt;SecondClass&gt;</code>, where <code>SecondClass</code> is a wrapper class for <code>FirstClass</code> that exposes nearly identical properties, however for various reasons needs to inherit from an unrelated base class. For example:</p> <pre><code>// My API void IQueryable&lt;SecondClass&gt; GetCurrentRecords() { return from row in dataSource /* Linq query */ select new SecondClass(row); } // User of my API var results = GetCurrentRecords().Where(row =&gt; row.Owner = "Mike"); </code></pre> <p>Now I can make the above compile simply by using <code>AsQueryable</code> however I want to expose a "true" <code>IQueryable</code> that efficiently queries the database based on the API users query.</p> <p>I know that this isn't trivial (my wrapper IQueryable implementation needs to understand the relationship between the properties of <code>SecondClass</code> and <code>FirstClass</code>), and that it has nothing to do with the <code>Select</code> function, but it <em>seems</em> like it should be possible.</p> <p>How do I do this?</p> <p><strong>Note:</strong> I know that instead my API could just expose <code>FirstClass</code> along with a helper method to convert <code>FirstClass</code> to <code>SecondClass</code> for when the API user is "done" creating their query, but it feels messy and I don't like the idea of exposing my generated classes in this way. Also I'd like to know how to do the above anyway just from a purely academic standpoint.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload