Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8464693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:49:49+00:00 2026-06-10T14:49:49+00:00

I’m trying to create a query to get the voting results. There is a

  • 0

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
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:49:50+00:00Added an answer on June 10, 2026 at 2:49 pm

    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);
            }
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.