IQueryable and model binding magic?

DimitarMK

New Member
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.Everything works fine, and that's the point, I was expecting to see some kind of exception...This is the code:Repository\[code\] public IQueryable<JobDto> GetJobs() { var ctx = this.contextResolver.GetCurrentContext<pubsEntities>(); return ctx.jobs.Select(x => new JobDto { Description = x.job_desc, ID = x.job_id, Maximum = x.max_lvl, Minimum = x.min_lvl }); }\[/code\]As you can see I'm projecting my EF entity into a custom DTO and the properties are totally different.ASPX Code behind\[code\] public IQueryable<JobDto> gv_GetData() { return this.jobsRepository.GetJobs(); }\[/code\]ASPX\[code\] <asp:GridView runat="server" ID="gv" AllowPaging="true" AllowSorting="true" DataKeyNames="ID" AutoGenerateColumns="true" SelectMethod="gv_GetData" ItemType="QueryRepository.JobDto, QueryRepository"> <Columns> <asp:BoundField DataField="Description" HeaderText="My custom description" SortExpression="Description" /> </Columns> </asp:GridView>\[/code\]This works like a charm, paging and sorting out of the box 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)My question is:If my repository is returning \[code\]IQueryable<JobDto>\[/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\]).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. Can anyone help this poor soul =( to understand what's happening behind the scenes??I ran a SQL profile just to confirm that the query is being executed correctly in the database:\[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] > 0ORDER BY [Project1].[job_desc] DESC\[/code\]I'd love to see a Jon Skeet's answer here =)
 
Back
Top