【问题标题】:Sum one field in one table and display the result from both in the view?对一个表中的一个字段求​​和并在视图中显示两者的结果?
【发布时间】:2023-04-08 12:13:01
【问题描述】:

我有两张桌子。一个有帖子,一个有投票。两个表都包含 postId (PK/FK)。我想对投票表中的所有帖子进行分组,并对每个帖子的 postVote 求和。然后在视图中我想显示帖子和每个帖子的总投票数。如果该 postId 没有投票,我希望使用 0 作为默认值。

如果我在 post 表中有这些数据

+--------+---------+-------------+---------+
| PostId | Message | MessageDate | User_Id |
+--------+---------+-------------+---------+
| 1      | test 1  | 2016-01-01  | 1       |
+--------+---------+-------------+---------+
| 2      | test 2  | 2016-01-01  | 1       |
+--------+---------+-------------+---------+
| 3      | test 3  | 2016-01-01  | 1       |
+--------+---------+-------------+---------+

投票表

+--------+------------+---------+
| PostId | PostVote   | User_Id |
+--------+------------+---------+
| 1      | 1          | 1       |
+--------+------------+---------+
| 1      | 1          | 2       |
+--------+------------+---------+
| 2      | -1         | 1       |
+--------+------------+---------+

我想在视图中显示这个

Message  Vote
Test 1 -  2
Test 2 -  -1
Test 3 -  0

由于两个用户对 postId 投了 1 (1+1) 票,因此有一个用户对 postId 2 投了 -1 票,并且对 PostId 3 没有投票(因此在视图中应该默认为 0)。

这是我的帖子模型

public class Post
{
    public Post()
    {
        this.Vote = new HashSet<Vote>();
    }

    public int PostId { get; set; }
    public string Message { get; set; }
    public DateTime MessageDate { get; set; }

    public virtual ApplicationUser User { get; set; }
    public virtual ICollection<Vote> Vote { get; set; }
}

这是我的投票模型

public class Vote
{
    [Key, Column(Order = 0)]
    public string ApplicationUserID { get; set; }

    [Key, Column(Order = 1)]
    public int PostId { get; set; }

    public virtual Post Post { get; set; }
    public virtual ApplicationUser ApplicationUser { get; set; }

    public int PostVote { get; set; }
}

我想我应该使用 Viewmodel 并创建它

public class ListPostsViewModel
{
    public List<Post> Posts { get; set; }
    public List<Vote> Votes { get; set; }
}

然后我尝试在控制器中使用我的 ViewModel,根据 postId 汇总投票并将帖子和投票合并到视图中。这是我现在的控制器。

var posts = db.Posts.OrderByDescending(p => p.PostId).ToList();

var postsVM = posts.Select(post => new ListPostsViewModel { Posts = posts});

var votes = db.Votes.GroupBy(u => u.PostId)
                .Select(v =>
                                new
                                {
                                    PostId = v.Key,
                                    TotalVotes = v.Sum(w => w.PostVote) //sum the votes
                                }).ToList();

var votesVM = posts.Select(post => new ListPostsViewModel { Votes = votes });

我在 votesVM 上收到此错误

不能隐式转换类型 'System.Collections.Generic.List' 到 'System.Collections.Generic.List'

【问题讨论】:

  • 您的查询正在创建匿名对象的集合。您需要将查询投影到您使用的模型中 - .Select(v =&gt; new SomeModel { PostId = v.Key, ... 但您当前的 Vote 类没有 TotalVotes 的属性,因此您需要一个新模型。并且不清楚您的 ListPostsViewModel 将如何生成该视图 - 您的模型需要是具有 string Messageint TotalVotes 属性的模型的集合
  • 您需要一个带有连接的查询(在PostId 字段上)并从Post 表中选择Message 字段以及从@987654338 中选择PostVote 字段的总和@表。
  • 你能给我举个例子吗?
  • 需要睡一会,如果还没解决,早上再补充答案
  • 我猜我的控制器(和视图模型)是错误的。我不想拥有 TotalVotes 的属性,它是从投票到视图的计算值,我没有保存在数据库中。 TotalVotes 是具有相同 PostId 的所有投票的总和。

标签: c# asp.net-mvc asp.net-mvc-4 viewmodel


【解决方案1】:

引发异常是因为您的 var votes = ... 查询正在生成一组匿名对象,然后您尝试将其分配给 List&lt;Vote&gt; 的属性(它们不是同一类型)。

为了生成你想要的视图,你的视图模型需要是

public class PostVoteVM
{
    public string Message { get; set; }
    public int TotalVotes { get; set; }
}

这样你就可以在视图中使用

@model IEnumerable<PostVoteVM>
<table>
    @foreach(var post in Model)
    {
        <tr>
            <td>@post.Message</td>
            <td>@post.TotalVotes </td>
        </tr>
    }
</table>

由于您的模型具有正确的导航属性,您只需要一个查询,并将查询结果投影到视图模型的集合中

var model = db.Posts.Include(p => p.Vote) // Include may not be required
    .OrderByDescending(p => p.PostId)
    .Select(p => new PostVoteVM()
    {
        Message = p.Message,
        TotalVotes = p.Vote.Sum(v => v.PostVote)
    });
return View(model);

【讨论】:

    猜你喜欢
    • 2015-09-02
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多