Reputation: 3515
If I'm sending object named Property which have Photos collection when I'm need only first element from that Photos collection like this, does this approach load all elements from collection and than assoc. only first like I want, or is this approach the one I need, first load from collection.
public PropertyHPViewModel(Property x)
{
Id = x.Id;
Created = x.Created;
Title = x.Title;
Photo = x.Photos.First();
}
Generated Sql from profiler
SELECT photos0_.PropertyId as PropertyId1_,
photos0_.Id as Id1_,
photos0_.Id as Id1_0_,
photos0_.ImageData as ImageData1_0_,
photos0_.ImageMimeType as ImageMim3_1_0_,
photos0_.PropertyId as PropertyId1_0_
FROM Photo photos0_
WHERE photos0_.PropertyId = 129 /* @p0 */
Updated Since I have 20 of this generated queries all differeted only in last line
WHERE photos0_.PropertyId = xxx /* @p0 */
where the number is photo id object, I must add that I have SELECT N+1 ALERT on this queries according to nhibernate profiler.
Upvotes: 2
Views: 90
Reputation: 171206
If x.Photos is not IQueryable, the .First() call cannot take part in the SQL query sent by your ORM. I suspect this is the case.
The only solution is to construct a query manually from the ISession.
Upvotes: 1
Reputation: 28699
When the expression tree is collapsed, NHibernate should generate SQL to only retrieve a single entity.
Upvotes: 0