Toad
Toad

Reputation: 15935

Query help for voting app in Linq using SQL Server

I'm trying to create a query to get the voting results.

There is a table which records every vote someone makes. It is possible for people to vote multiple times, but only the last vote should be taken into account.

I'm at the point that it works, except the 'only the last vote of a user should be taken into account' part.

This is my table:

Table Vote:

  id            bigint      PK
  TimeStamp     datetime
  QuestionId    int
  UserId        int

and the Linq query:

var total = (float)(_db.Votes).Count(vote => vote.Timestamp>=startWeekDay && vote.Timestamp<endWeekDay);

var results = (from vote in _db.Votes 
                  where vote.Timestamp >= startWeekDay && vote.Timestamp < endWeekDay
                  group vote by new { vote.QuestionId} into g
                  orderby g.Count() descending
                  select new VoteResults()
                      {
                          QuestionID = g.Key.QuestionId, 
                          Percentage = (g.Count() / total * 100.0f),
                      }).ToList();

My question: how do I change this query so that only the last cast vote per user is taken into account?

EDIT: Given this input:

{
    UserId = 1,
    QuestionId = 2,
    TimeStamp = Yesterday
},
{
    UserId = 1,
    QuestionId = 1,
    TimeStamp = Today
},
{
    UserId = 2,
    QuestionId = 2,
    TimeStamp = Yesterday
},
{
    UserId = 3,
    QuestionId = 1,
    TimeStamp = Yesterday
}

The output should look like this:

{
    QuestionID = 2,
    Percentage = 33.3333
},
{
    QuestionID = 1,
    Percentage = 66.66666
}

Upvotes: 2

Views: 117

Answers (1)

Leniel Maccaferri
Leniel Maccaferri

Reputation: 102448

Bellow I show you a test case I ran in a Console App... It should solve your problem. I tested with the data you passed in your example:

public class User
{
    public int UserId {get; set;}
}

public class Question
{
    public int QuestionId { get; set; }
}

public class Vote
{
    public int VoteId {get; set;}
    public DateTime TimeStamp {get; set;}
    public Question Question {get; set;}
    public User User { get; set; }
}

public class VoteResult
{
    public int QuestionId { get; set; }
    public float Percentage { get; set; }
}

public class Program
{
    static void Main(string[] args)
    {
        var question1 = new Question();
        question1.QuestionId = 1;

        var question2 = new Question();
        question2.QuestionId = 2;

        var user1 = new User();
        user1.UserId = 1;

        var user2 = new User();
        user2.UserId = 2;

        var user3 = new User();
        user3.UserId = 3;

        List<Vote> votes = new List<Vote>()
        {
            new Vote()
            { 
                Question = question2,
                TimeStamp = DateTime.Today.AddDays(-1), // Yesterday
                User = user1,
                VoteId = 1
            },

            new Vote()
            { 
                Question = question1,
                TimeStamp = DateTime.Today,
                User = user1,
                VoteId = 2
            },

            new Vote()
            {
                Question = question2,
                TimeStamp = DateTime.Today.AddDays(-1), // Yesterday
                User = user2,
                VoteId = 3
            },

            new Vote()
            {
                Question = question1,
                TimeStamp = DateTime.Today.AddDays(-1), // Yesterday
                User = user3,
                VoteId = 4
            }
        };

        // Group Votes by User and then Select only the most recent Vote of
        // each User
        var results = from vote in votes
                        where vote.TimeStamp >= DateTime.Today.AddDays(-7) &&
                              vote.TimeStamp < DateTime.Today.AddDays(1)
                        group vote by vote.User into g
                        select g.OrderByDescending(v => v.TimeStamp).First();

        // Total Users who voted
        var total = results.Count();

                               // Group Users' votes by Question
        foreach (var result in results.GroupBy(v => v.Question.QuestionId))
        {
            var voteResult = new VoteResult()
            {
                QuestionId = result.Key,
                Percentage = ((float)result.Count() / total) * 100.0f
            };

            Console.WriteLine(voteResult.QuestionId);

            Console.WriteLine(voteResult.Percentage);
        }

    }
}

Upvotes: 2

Related Questions