MikeKulls
MikeKulls

Reputation: 3049

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?

Upvotes: 1

Views: 447

Answers (1)

Eranga
Eranga

Reputation: 32437

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.

Upvotes: 1

Related Questions