Loading navigation properties in NHibernate 3.2 -


i have these entities:

public class post {     public virtual int id { get; set; }     public virtual icollection<tag> tags { get; set; }     public virtual icollection<comment> comments { get; set; } }  public class tag {     public virtual int id { get; set; }     public virtual icollection<post> posts { get; set; } }  public class comment {     public virtual int id { get; set; }     public virtual post post { get; set; } } 

i want load post it's related tags , related comments's count linq. use this:

var posts = session     .query<post>()     .orderby(t => t.id)     .select(t => new {         post = t,         commentscount = t.comments.count(),         tags = t.tags             }).tolist(); 

do think enough? or have suggestion may better code? thanks.

this highly depends on mapping , want fetched result. imho eager loading tags speed performance (select n+1) if want access tags list (assuming tags mapped lazy).

var posts = session     .query<post>()     .fetch(t => t.tags)     .orderby(t => t.id)     .select(t => new {         post = t,         commentscount = t.comments.count(),         tags = t.tags             }).tolist(); 

http://ayende.com/blog/1328/combating-the-select-n-1-problem-in-nhibernate


Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -