views:

44

answers:

2

I have an entity called Incident and a DTO called IncidentDTO. For now, IncidentDTO simply looks like this:

public class IncidentDTO : Incident
{
    // empty until I can get AutoMapper working correctly
}

I'm trying to pull a list of all the Incidents in the database and convert them to DTOs using this code:

Mapper.CreateMap<Incident, IncidentDTO>();

using (var session = SessionFactory.OpenSession())
{
    var incidents = session.Linq<Incident>();
    var incidentDTOs = Mapper.Map(incidents, new List<IncidentDTO>());
}

This code works fine, except when I use NHProf to look at the SQL statements being generated, I get this:

SELECT ... FROM [Incident] this_
SELECT ... FROM [Incident] this_

The two SELECT statements are exactly identical. Why does AutoMapper generate two identical SELECT statements, and how do I prevent it from doing this?

A: 

I think your problem is related with this issue:

Neuquino
Unfortunately, it's not a SELECT N+1 problem. The `Incident` is not associated with anything else.
Daniel T.
+1  A: 

A guess: Enumerating IQueryable creates a separate select for every item. Solve it with enumerating IList.

var incidents = session.Linq<Incident>().ToList();

I would probably do this to prevent another problem.

int someReasonableNumber = 1000;
var incidents = session.Linq<Incident>().Take(someReasonableNumber).ToList();

This is just a guess, not something I really know.

Paco
That did it, thanks! I didn't consider enumerating the queryable first. As for the unbound set, that's not a problem because I don't foresee the `Incident` count ever going over 200.
Daniel T.