Using LinqPad I found something else. I can write

var t=from p in NHUnitOfWork.CurrentSession.Linq<Post>()
select new //MyDTO
        {
Title=p.Title,
CommentsCount=p.Comments.Count()
        };
t.Dump();
}

And this works ok, result looks like I want. But when I want to return
result to MyDTO object, in LinqPad I have exception:
Column 'Posts.Title' is invalid in the select list because it is not
contained in either an aggregate function or the GROUP BY clause.

With linq to sql it works ok.
How can I write it to return result to MyDTO with linq to nhibernate?

On 30 Paź, 00:20, lszk <[email protected]> wrote:
> 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