了解用户是否喜欢特定评论

Find out if a user liked specific comment

我有以下 Post class:

public class Post
{
  public string Id {get;set;}
  public string Content {get;set;}
  public IList<Comment> Comments {get;set;}
}

public class Comment
{
  public int OrderNumber {get;set;} //kind of CommentId
  public string AuthorId {get;set;}
  public string Text {get;set;}
  public IList<string> Voters {get;set;} //Ids of users who liked the post
}

每条评论可能有数千个点赞,但我只需要在客户端显示它们的数量,所以我创建了以下索引:

Map = posts => from post in posts
               select new {
                            Id = post.Id,
                            Content = post.Content, 
                            Comments = post.Comments.Select(x => 
                              new CommentProjection {
                                AuthorId = x.AuthorId,
                                Text = x.Text,
                                VotersCount = x.VotersCount
                                                    }),
                          };

问题是我需要突出显示用户已经喜欢的那些评论。有没有办法修改上述索引以将此信息添加到查询结果中?

你可以反过来做,跟踪用户的评论,这样你就可以显示他们喜欢的评论。

为什么不创建一个名为 Votes 的单独实体?它可以有 3 个简单的字段,一个指向用户 table 的外键,另一个指向您的评论 table。第三个(位)表示这是反对票还是赞成票(喜欢)。

然后您可以直接轻松地查询此 table(通过过滤用户 ID 和带有 "upvote" 的用户 ID)