Is this possible to use let keyword with nhibernate linq? I wrote

posts = from post in postsRepository.GetPosts(name)
                    let commentsCount = (from c in
NHUnitOfWork.CurrentSession.Linq<Comment>()
                 where c.Post.ID == post.ID
                 select c).Count()
                    select new ...

and in response I have NHibernate.QueryException: could not resolve
property: post of: Sys.Domain.Entities.Post

I tried also to write it without the let expression.

 posts = from post in postsRepository.GetPosts(name)
                        select new
PublishedPostsWithCategoryAndTagsDTO()
                        {

                            CommentsCount =
                            (from c in post.Comments
                             where c.Post.ID == post.ID
                             select c).Count(),

Result is similar:
NHibernate.QueryException: could not resolve property: Comments.ID of:
Sys.Domain.Entities.Post

In the end the third:

 posts = from post in postsRepository.GetPosts(name)
                        let commentsCount =
 
commentsRepository.GetCommentsCountForPost(post.ID)
//...

 public int GetCommentsCountForPost(Guid? postId)
        {
            var t = (from c in Session.Linq<Comment>()
                     where c.Post.ID.Value == postId.Value
                     select c).Count();

            return t;
        }

The result:
System.InvalidCastException: You cannot cast
'NHibernate.Linq.Expressions.PropertyAccessExpression' to
'NHibernate.Linq.Expressions.CollectionAccessExpression'.


Only this one works. Take the posts and use a loop:
 foreach (var t in posts)
                {
                  t.CommentsCount=(from c in
NHUnitOfWork.CurrentSession.Linq<Comment>()

                     where c.Post.ID == t.ID
                    select c).Count();
                }
Unfortunatelly select n+1 occurs.

How can I do it without loop? post->comment this is one to many
relation. I need this too for many to many, so I hope this is posible
to do.

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to