LINQ -使用UserId和Guid查找两个表之间的连接,并重新调整投票计数

6l7fqoea  于 2023-06-19  发布在  其他
关注(0)|答案(3)|浏览(138)
tblQuestion
-----------
questionId     question       userId
--------------------------------------
1              question 1     1
2              question 2     2

tblVotes
--------
voteId         userId        questionId
----------------------------------------
1              1             1
2              2             1
3              3             1
4              1             2

正如你在上面看到的,我有两个表,问题表和投票表。每个问题可以具有在投票表中表示的多个投票,因为投票表链接到问题Id。
如果我在服务器端有一个这样的函数:

public IActionResult GetPollsVoted(string userId)

我希望传递userId,以便结果应该是,如果传入的userId等于“1”:

tblResults
----------
questionId      question      userId    voteCount
-------------------------------------------------
1               question 1    1         3
2               question 2    1         1

我知道我仍然在试图让我的头围绕这个场景,因为我试图使用linq在c#中产生上面的结果,那么我如何才能实现这个使用link来获得tblResults答案的reusltgs呢?
我试过这个,但似乎仍然不能让这个linq命令工作:

polls1 = from c in ctx.TblPolls
         join o in ctx.TblVotes on c.UserId equals o.UserId into g
         group c.UserId by c.Guid into gg
         from d in ctx.TblPolls
         join m in ctx.TblVotes on d.Guid equals m.PollGuid into ggg
         select new { d.Question, Count = ggg.Count() };

linq中的表与我展示的示例不对应,但是tblQuestions等于ctx.TblPolls,tblVotes等于ctxTblVotes,d.Guid等于tblQuestion中的questionId,m.PollGuid等于tblVotes中的questionId,作为到这两个表的链接。userId是自我解释的。
那么我如何使用linq得到我想要的结果呢?

zhte4eai

zhte4eai1#

我想我已经把你们的名字整理好了。以下是你发布的答案的一个稍微更可读的版本:

var TblPolls = new[]
{
    new { Guid = 1, Question = "question 1", UserId = 1 },
    new { Guid = 2, Question = "question 2", UserId = 2 },
};

var TblVotes = new[]
{
    new { VoteId = 1, UserId = 1, PollGuid = 1 },
    new { VoteId = 2, UserId = 2, PollGuid = 1 },
    new { VoteId = 3, UserId = 3, PollGuid = 1 },
    new { VoteId = 4, UserId = 1, PollGuid = 2 },
};

var userId = 1;

var polls =
(
    from vote in TblVotes
    where vote.UserId == userId
    join poll in TblPolls on vote.PollGuid equals poll.Guid
    join vote2 in TblVotes on poll.Guid equals vote2.PollGuid into votes
    select new
    {
        poll.Question,
        Count = votes.Count(),
    }
).ToList();
kmpatx3s

kmpatx3s2#

您的示例数据或数据类型与您的查询不同。我试着用例子和你问题

public IActionResult GetPollsVoted(string userId)

{
            int _userid = int.Parse(userId);
var _listuser = _db.TblPolls
                .Join(_db.TblVotes,
                      p => p.Id,
                      e => e.questionId,
                      (p, e) => new
                      {
                          Polls = p,
                          Votes = e
                      }
                      ).Where(d => d.Votes.userId == _userid && d.Polls.userId == _userid)
                      .GroupBy(d => d.Votes.questionId).Select(d => new
                      {
                          questionId = d.Key,
                          question = d.FirstOrDefault().Polls.question,
                          userId = _userid,
                          voteCount = d.Count()

                      }).ToList();

结果:
| questionId|提问|用户ID| voteCount|
| - -----|- -----|- -----|- -----|
| f539fdbb-3ff2-4d00-85f3-520e018e8bcb|问题1| 1| 3|
示例日期

Guid iD = Guid.NewGuid();
Guid iD2 = Guid.NewGuid();

_db.TblPolls.Add(new TblPoll() { Id= iD, userId = 1, question = "question 1"});
 _db.TblPolls.Add(new TblPoll() { Id = iD2, userId = 2, question = "question 2" });

_db.TblVotes.Add(new TblVote() { userId = 1, questionId  = iD });
_db.TblVotes.Add(new TblVote() { userId = 1, questionId = iD });
_db.TblVotes.Add(new TblVote() { userId = 1, questionId = iD });
_db.TblVotes.Add(new TblVote() { userId = 2, questionId = iD2 });
xcitsw88

xcitsw883#

我设法得到了答案:

var votesCount = ctx.TblVotes.Where(x => x.UserId == userId).Count();
var votes = ctx.TblVotes.Where(x => x.UserId == userId).ToList();
polls = votes.Where(c => c.UserId == userId).Join(ctx.TblPolls, x => x.PollGuid, y => y.Guid, (x, y) => new { y.Question, vCount = ctx.TblVotes.Where(x => x.PollGuid == y.Guid).Count() }).ToList();

相关问题