如何:在 Linq to sql 中对所有值求和并分配总数的百分比

发布于 2024-12-01 16:13:53 字数 537 浏览 1 评论 0原文

我有一个简单的 linq 查询,我试图扩展它,以便我可以首先对 VoteCount 字段中的所有值求和,然后对于每个被提名者,我想分配被提名者收到的选票百分比。

这是代码:

              TheVoteDataContext db = new TheVoteDataContext();
    var results = from n in db.Nominees
                  join v in db.Votes on n.VoteID equals v.VoteID
                  select new
                  {
                      Name = n.Name,
                      VoteCount = v.VoteCount,
                      NomineeID = n.NomineeID,
                      VoteID = v.VoteID
                  };

I have a simple linq query that I'm trying to extend so that I can first sum all the values in the VoteCount field and then for each Nominee I want to assign what percentage of votes the nominee received.

Here's the code:

              TheVoteDataContext db = new TheVoteDataContext();
    var results = from n in db.Nominees
                  join v in db.Votes on n.VoteID equals v.VoteID
                  select new
                  {
                      Name = n.Name,
                      VoteCount = v.VoteCount,
                      NomineeID = n.NomineeID,
                      VoteID = v.VoteID
                  };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

终难遇 2024-12-08 16:13:53

由于为每个被提名者选择单票和计算所有票数的总和是两个不同的任务,因此我想不出一种在单个查询中有效地完成此操作的方法。我只需分两步完成,因为

var results = from n in db.Nominees
              join v in db.Votes on n.VoteID equals v.VoteID
              select new
              {
                  Name = n.Name,
                  VoteCount = v.VoteCount,
                  NomineeID = n.NomineeID,
                  VoteID = v.VoteID
              };

var sum = (decimal)results.Select(r=>r.VoteCount).Sum();
var resultsWithPercentage = results.Select(r=>new {
                               Name = r.Name,
                               VoteCount = r.VoteCount,
                               NomineeID = r.NomineeID,
                               VoteID = r.VoteID,
                               Percentage = sum != 0 ? (r.VoteCount / sum) * 100 : 0
                            });

您还可以在结果之前计算总和(使用聚合查询),这会将求和的任务留给数据库引擎。我相信这会更慢,但你总是可以通过尝试找到答案:)

Since selecting the single votes for each nominee and calculating the sum of all votes are two different tasks, I cannot think of a way of doing this efficiently in one single query. I would simply do it in two steps, as

var results = from n in db.Nominees
              join v in db.Votes on n.VoteID equals v.VoteID
              select new
              {
                  Name = n.Name,
                  VoteCount = v.VoteCount,
                  NomineeID = n.NomineeID,
                  VoteID = v.VoteID
              };

var sum = (decimal)results.Select(r=>r.VoteCount).Sum();
var resultsWithPercentage = results.Select(r=>new {
                               Name = r.Name,
                               VoteCount = r.VoteCount,
                               NomineeID = r.NomineeID,
                               VoteID = r.VoteID,
                               Percentage = sum != 0 ? (r.VoteCount / sum) * 100 : 0
                            });

You could also calculate the sum before the results (using an aggregate query), this would leave the task of summing to the Database engine. I believe that this would be slower, but you can always find out by trying :)

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