IQueryable and model binding magic?
<p>I was testing the new ASP.NET 4.5 model binding for Web Forms, with a simple repository exposing an IQueryable. The repository is using EF 5, Database First approach. I'm projecting the EF auto-generated entity to use my DTO.</p>

<p>Everything works fine, and that's the point, I was expecting to see some kind of exception...</p>

<p>This is the code:</p>

<h3>Repository</h3>

<pre><code>    public IQueryable&lt;JobDto&gt; GetJobs()
    {
        var ctx = this.contextResolver.GetCurrentContext&lt;pubsEntities&gt;();

        return ctx.jobs.Select(x =&gt; new JobDto
            {
                Description = x.job_desc,
                ID = x.job_id,
                Maximum = x.max_lvl,
                Minimum = x.min_lvl
            });
    }
</code></pre>

<p>As you can see I'm projecting my EF entity into a custom DTO and the properties are totally different.</p>

<h3>ASPX Code behind</h3>

<pre><code>    public IQueryable&lt;JobDto&gt; gv_GetData()
    {
        return this.jobsRepository.GetJobs();
    }
</code></pre>

<h3>ASPX</h3>

<pre><code>    &lt;asp:GridView runat="server" ID="gv" AllowPaging="true" AllowSorting="true"
        DataKeyNames="ID"
        AutoGenerateColumns="true"
        SelectMethod="gv_GetData"
        ItemType="QueryRepository.JobDto, QueryRepository"&gt;
        &lt;Columns&gt;
            &lt;asp:BoundField DataField="Description" HeaderText="My custom description" SortExpression="Description" /&gt;
        &lt;/Columns&gt;
    &lt;/asp:GridView&gt;
</code></pre>

<p>This works like a charm, paging and sorting <em>out of the box</em> when using a repository which is great (Now I do not have to use an <code>ObjectDataSource</code> to simplify things like paging and sorting)</p>

<p><strong>My question is:</strong></p>

<p>If my repository is returning <code>IQueryable&lt;JobDto&gt;</code> and the properties of my DTO do not have the same name than the properties of my EF entity (which is a different entity named: <code>job</code>).</p>

<p>How is it possible that EF can sort my <code>GridView</code> correctly since my <code>GridView</code> is configured with the property names defined in my DTO entity??? As far as I know, the dynamic sorting using LINQ is done using a string to set the order criteria. Somehow LINQ to Entities - <code>IQueryable</code> is auto-mapping my DTO properties to the properties exposed by my EF entity. </p>

<p>Can anyone help this poor soul =( to understand what's happening behind the scenes??</p>

<p>I ran a SQL profile just to confirm that the query is being executed correctly in the database:</p>

<pre><code>SELECT TOP (10) 
[Project1].[C1] AS [C1], 
[Project1].[job_desc] AS [job_desc], 
[Project1].[job_id] AS [job_id], 
[Project1].[max_lvl] AS [max_lvl], 
[Project1].[min_lvl] AS [min_lvl]
FROM ( SELECT [Project1].[job_id] AS [job_id], [Project1].[job_desc] AS [job_desc], [Project1].[min_lvl] AS [min_lvl], [Project1].[max_lvl] AS [max_lvl], [Project1].[C1] AS [C1], row_number() OVER (ORDER BY [Project1].[job_desc] DESC) AS [row_number]
    FROM ( SELECT 
        [Extent1].[job_id] AS [job_id], 
        [Extent1].[job_desc] AS [job_desc], 
        [Extent1].[min_lvl] AS [min_lvl], 
        [Extent1].[max_lvl] AS [max_lvl], 
        1 AS [C1]
        FROM [dbo].[jobs] AS [Extent1]
    )  AS [Project1]
)  AS [Project1]
WHERE [Project1].[row_number] &gt; 0
ORDER BY [Project1].[job_desc] DESC
</code></pre>

<p><strong>Pay special attention to these lines (ASPX):</strong></p>

<pre><code>&lt;asp:BoundField DataField="Description" SortExpression="Description" /&gt;
</code></pre>

<p><strong>And the resulting SQL</strong></p>

<pre><code>ORDER BY [Project1].[job_desc] DESC
</code></pre>
