LINQ - 如何选择和计算正确的结果
我有 3 个表:DiaryPosts、DiaryImpressions、Impressions。
印象是一个包含一些字段的小列表:“喜欢”、“不喜欢”、“同意”、“不同意”。用户可以根据自己对帖子的看法进行投票。
DiaryImpressions 是处理帖子和投票用户之间关系的表。
我的问题是,我必须计算每个帖子的每次印象投票的结果,所以对于一篇帖子,可能有 13 个用户投票喜欢,2 个用户投票不喜欢,34 个用户同意,1 个不喜欢。
我不知道如何以某种方式执行查询,我可以获得每次展示的特定计数结果。
有人能帮我吗?
I have 3 tables: DiaryPosts, DiaryImpressions, Impressions.
Impressions is a small list with some fields: 'like', 'dislike', 'agree', 'disagree'. The user can vote according to what he thinks about the post.
DiaryImpressions is the table that handles the relationship between the posts and the users who vote.
My problem is, I have to count results of each impression vote for each post, so maybe for one post 13 users voted for like, 2 for dislike, 34 agree and 1 disagree.
I have no idea how to perform the query in some way I can get this specific count results for each impression.
Can anyone help me with that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过 GroupBy 方法来执行此操作。它允许您根据展示次数自动对元素进行“分组”,并计算每组的结果。
You can do this via the GroupBy method. It allows you to automatically "group" the elements based on the impression, and count the results per group.
它应该计算每个帖子的印象数,并计算喜欢或不喜欢的用户数量,因此,如果每个帖子有 30 个人不喜欢,它应该得到他们我看不到你的表格结构,但我希望它能帮助或指出你的地方
it should count the impressions for each post and count the amount of users that like or dislike so if you have 30 people dislike for each post it should get them I cannot see your table structure but I hope it will help or point you somewhere
我必须猜测,但我认为 SQL 会是什么样子:
以及 linq 会是什么样子:
当然,我必须假设你的列表是以某种方式创建的,如果你可以发布你的列表是如何实际创建的,那么帮助。 (正如我在评论中所说)
I have to guess but here is what I think the SQL would look like:
And what the linq would look like:
Of course I have to assume your list is created a certain way, if you can post how your list is actually created that would help. (As I said in the comments)