使用GroupBy的NHibernate LINQ查询

使用GroupBy的NHibernate LINQ查询,linq,nhibernate,Linq,Nhibernate,我正在努力将SQL转换为NHibernate HQL SQL查询 SELECT Posts.id, Count(Comments.id) FROM Posts LEFT JOIN Comments ON Posts.id=Comments.fk_post GROUP BY Posts.id 林克 Session.Query().Fetch(x=>x.Comments).GroupBy(x=>x.id,x=>x.Comments) .Select(x=>new{PostId=x.Key,Comm

我正在努力将SQL转换为NHibernate HQL

SQL查询

SELECT Posts.id, Count(Comments.id) FROM Posts LEFT JOIN Comments ON Posts.id=Comments.fk_post GROUP BY Posts.id
林克

Session.Query().Fetch(x=>x.Comments).GroupBy(x=>x.id,x=>x.Comments)
.Select(x=>new{PostId=x.Key,CommentCount=x.Single().Count});
这仍然是失败的,只有一个例外: 参数“inputInfo”的类型为“Remotion.Linq.Clauses.StreamedData.StreamedSingleValueInfo”,而类型应为“Remotion.Linq.Clauses.StreamedData.StreamedSequenceInfo”

我的查询有什么问题?

尝试以下查询:

var查询=
来自Session.Query()中的p
来自p.Comments.DefaultIfEmpty()中的c
c组按p.Id分为g组
选择新的
{
PostId=g.键,
CommentCount=g.Sum(x=>(int?)c.Id==null?0:1)
}
var result=query.ToList();;
尝试此查询:

var查询=
来自Session.Query()中的p
来自p.Comments.DefaultIfEmpty()中的c
c组按p.Id分为g组
选择新的
{
PostId=g.键,
CommentCount=g.Sum(x=>(int?)c.Id==null?0:1)
}
var result=query.ToList();;

所以你有
帖子
评论
的表格。帖子和评论之间存在一对多的关系:每个帖子都有零个或多个评论,每个评论都只属于一个帖子,即外键
Comments.fk_Post
所指的帖子

您希望获取每个帖子的Id以及该帖子的评论数

每当你需要选择“零个或多个子项目”时,比如学校和学生、客户的订单,或者在你的案例中贴上他们的评论,考虑使用其中的一个重载。

如果您看到一个SQL左外部联接后跟一个GroupBy,您还可以看到GroupJoin是最明显的解决方案。 每当您看到SQL左外部联接后跟GroupBy时,几乎可以肯定您需要GroupJoin

如果您想要的不是juse“items及其子项”,请使用带有参数resultSelector的重载

我不知道nHibernate,我假设
会话
查询
获取
用于获取iQueryTables。由于这不是问题的一部分,我让您自行获取IQueryables:

IQueryable<Post> posts = ...
IQueryable<Comment> comments = ...

// GroupJoin Posts with Comments
var postIdsWithCommentsCount = posts.GroupJoin(comments,

post => post.Id,               // from every Post take the primary key
comment => comment.fk_post,    // from every Comment take the foreign key to Post

// parameter resultSelector: from every Post, with all its zero or more Comments,
// make one new
(post, commentsOfThisPost) => new
{
    Id = post.Id,
    Count = commentsOfThisPost.Count(),
});
IQueryable posts=。。。
IQueryable注释=。。。
//群发评论
var postedswithcommentscont=posts.GroupJoin(注释,
post=>post.Id,//从每个post获取主键
comment=>comment.fk_post,//从每个注释中获取外键进行post
//参数resultSelector:从每个帖子及其零条或多条评论中,
//做一个新的
(post,commentsOfThisPost)=>新建
{
Id=post.Id,
Count=commentsOfThisPost.Count(),
});

所以你有
帖子
评论
的表格。帖子和评论之间存在一对多的关系:每个帖子都有零个或多个评论,每个评论都只属于一个帖子,即外键
Comments.fk_Post
所指的帖子

您希望获取每个帖子的Id以及该帖子的评论数

每当你需要选择“零个或多个子项目”时,比如学校和学生、客户的订单,或者在你的案例中贴上他们的评论,考虑使用其中的一个重载。

如果您看到一个SQL左外部联接后跟一个GroupBy,您还可以看到GroupJoin是最明显的解决方案。 每当您看到SQL左外部联接后跟GroupBy时,几乎可以肯定您需要GroupJoin

如果您想要的不是juse“items及其子项”,请使用带有参数resultSelector的重载

我不知道nHibernate,我假设
会话
查询
获取
用于获取iQueryTables。由于这不是问题的一部分,我让您自行获取IQueryables:

IQueryable<Post> posts = ...
IQueryable<Comment> comments = ...

// GroupJoin Posts with Comments
var postIdsWithCommentsCount = posts.GroupJoin(comments,

post => post.Id,               // from every Post take the primary key
comment => comment.fk_post,    // from every Comment take the foreign key to Post

// parameter resultSelector: from every Post, with all its zero or more Comments,
// make one new
(post, commentsOfThisPost) => new
{
    Id = post.Id,
    Count = commentsOfThisPost.Count(),
});
IQueryable posts=。。。
IQueryable注释=。。。
//群发评论
var postedswithcommentscont=posts.GroupJoin(注释,
post=>post.Id,//从每个post获取主键
comment=>comment.fk_post,//从每个注释中获取外键进行post
//参数resultSelector:从每个帖子及其零条或多条评论中,
//做一个新的
(post,commentsOfThisPost)=>新建
{
Id=post.Id,
Count=commentsOfThisPost.Count(),
});

谢谢您的回复。我只需将g.Sum(x=>c.Id)更改为.Sum(x=>x.Id)是否可以将生成的内部联接更改为左联接?更正的代码。感谢您的回复。我只需将g.Sum(x=>c.Id)更改为.Sum(x=>x.Id)是否可以将生成的内部联接更改为左联接?更正的代码。