我有两个表:comments 和commentLikes 在同一个查询中我计算用户对评论的喜欢。
我得到了以下(简化的)查询:
var res = (from c in db.Comments
where c.Topic.ID == topicID
select new
{
comment = c,
count = c.CommentLikes.Count()
}).ToList();
但是,我不想再次将likecount 映射到评论实体中,而是希望获得一个评论列表,其中仅包含一个LikeCount 字段,最好使用有效的查询。像这样的东西:
var res = (from c in db.Comments
where c.Topic.ID == topicID
select new
{
comment = c,
c.LikeCount = c.CommentLikes.Count()
}).ToList();
此查询无法编译。
如何在linq中做到这一点?