Entity Framework - get multiple results from an SQL statement

With EF I can return a collection of objects like so

entities.Customers.ToArray();

And I can include other tables, so I can effectively get 2 result sets back in the one query

entities.Customers.Include("Invoice").ToArray();

or if I have some custom SQL I can achive a similar result:

SqlDataReader reader = GetReaderFromSomewhere("SELECT * FROM Customer");
entities.Translate<Customer>(reader).ToArray();

But how do I get multiple results back from my own SQL? What I was thinking was something like this

SqlDataReader reader = GetReaderFromSomewhere("SELECT Customer.Name AS CustomerName, Invoice.Number AS InvoiceNumber FROM Customer JOIN Invoice ON Customer.ID = Invoice.CustomerID");
entities.Translate<Customer>(reader).Include<Invoice>().ToArray();

In the above example I have prefixed all the returned data with the table name so that the Translate method can know which columns belong to which tables. I'm presuming the Tranlate method does not support this but EF must do something similar when the include method is called. So my question is, how can I get the functionality of Include when using Translate?

Answers


AFAIK you can not do that manually. What EF does is, it dynamically generate a class based on the LINQ query(and the Includes) that can materialize entities. Basically this class knows which columns will be mapped to which properties.

However you can use a micro ORM like Dapper which can do Multi Mapping. But this will only work for querying. So change tracking and CUD operations will not be available.


Need Your Help

Angularjs and Highlightjs - showing variables

javascript angularjs

I'm trying to get Angularjs and Hightlightjs to play nicely with each other and I found this: https://github.com/pc035860/angular-highlightjs

How to save trace info into a file

asp.net web-config trace

I am tracing my ASPX page by enabling tracing in web.config (&lt;trace enabled="true" pageOutput="true"/&gt;)