LINQ - 如何选择和计算正确的结果

发布于 2024-12-13 23:43:10 字数 309 浏览 0 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

安穩 2024-12-20 23:43:10

您可以通过 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.

孤者何惧 2024-12-20 23:43:10
var post(from c in posts selec c)

string post1like;
string post2dislike;
string postagree3;
string postdisagree3;


foreach (var item in posts)

{
  var ipressionslike=(from c in imressions where impressioid=item.id where c.impressionID='LIKE' select c.userID).Tolist()
   var ipressionsdislike=(from c in imressions where impressioid=item.id where c.impressionID='disLIKE' select c.userID).Tolist()   
  var ipressionslike=(from c in imressions where impressioid=item.id where c.impressionID='LIKE' select c.userID).Tolist()
   var ipressionsagree=(from c in imressions where impressioid=item.id where c.impressionID='disLIKE' select c.userID).Tolist()  
post1like+=item.id+""+ipressionsdislike.count;
post2dislike+=item.id+""+ipressionslike.count;
postagree3+=item.id+""+ipressionsagree.count;
}

它应该计算每个帖子的印象数,并计算喜欢或不喜欢的用户数量,因此,如果每个帖子有 30 个人不喜欢,它应该得到他们我看不到你的表格结构,但我希望它能帮助或指出你的地方

var post(from c in posts selec c)

string post1like;
string post2dislike;
string postagree3;
string postdisagree3;


foreach (var item in posts)

{
  var ipressionslike=(from c in imressions where impressioid=item.id where c.impressionID='LIKE' select c.userID).Tolist()
   var ipressionsdislike=(from c in imressions where impressioid=item.id where c.impressionID='disLIKE' select c.userID).Tolist()   
  var ipressionslike=(from c in imressions where impressioid=item.id where c.impressionID='LIKE' select c.userID).Tolist()
   var ipressionsagree=(from c in imressions where impressioid=item.id where c.impressionID='disLIKE' select c.userID).Tolist()  
post1like+=item.id+""+ipressionsdislike.count;
post2dislike+=item.id+""+ipressionslike.count;
postagree3+=item.id+""+ipressionsagree.count;
}

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

晨曦慕雪 2024-12-20 23:43:10

我必须猜测,但我认为 SQL 会是什么样子:

SELECT I.Name, COUNT(I.Name)
FROM DairyPosts P
JOIN DairyImpressions DI ON P.ID=DI.DairyID
JOIN Impressions I ON DI.ImpressionsID = I.ID

以及 linq 会是什么样子:

List<Dairy> dairyposts = GetDairyPosts();
var impressionCounts =
    from p in dairyposts
    group p by p.ImpressionName into g
    select new { Type = g.Key, ImpressionCount = g.Count() };

当然,我必须假设你的列表是以某种方式创建的,如果你可以发布你的列表是如何实际创建的,那么帮助。 (正如我在评论中所说)

I have to guess but here is what I think the SQL would look like:

SELECT I.Name, COUNT(I.Name)
FROM DairyPosts P
JOIN DairyImpressions DI ON P.ID=DI.DairyID
JOIN Impressions I ON DI.ImpressionsID = I.ID

And what the linq would look like:

List<Dairy> dairyposts = GetDairyPosts();
var impressionCounts =
    from p in dairyposts
    group p by p.ImpressionName into g
    select new { Type = g.Key, ImpressionCount = g.Count() };

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文