简体   繁体   中英

Display sum of values from database in view MVC 4

I have a table Attendance:

public class Attendance
    {
        public virtual int AttendanceId { get; set; }

        public virtual int UserCourseId { get; set; }
        public virtual UserCourse UserCourse { get; set; }

        public virtual int EventId { get; set; }
        public virtual Event Event { get; set; }

        public virtual int presence { get; set; } 
        public virtual bool check { get; set; }
    }

And a table UserCourses that relates the User from UserProfile to the table Courses:

public class UserCourse
    {
        public virtual int UserCourseId { get; set; }

        public virtual int UserId { get; set; }
        public virtual UserProfile User { get; set; }

        public virtual int CourseId { get; set; }
        public virtual Course Course { get; set; }

        public virtual List<Attendance> Attendance { get; set; }
    }

When a new attendance is created, presence turns into 1 if check is true and 0 if check is false. I need to get the SUM of the total os presences of each user and display in a view.

Example in database:

User1 - Event1 - True - 1
User2 - Event1 - False - 0
User3 - Event1 - True - 1
User1 - Event2 - True - 1
User2 - Event2 - True - 1
User3 - Event2 - True - 1

So in the view it would show the total number of presences:

User1 - 2
User2 - 1
User3 - 2

How can I do this? Thanks

To extend on @Shyju's comment and using your example data, I put this together:

var results = testData.GroupBy(user => user.UserId,
                               user => user.Attendance.Sum(a => a.presence),
                               (key, values) => "User" + key + " - " + values.First());

foreach (var result in results)
{
    System.Console.WriteLine(result);
}

or

var results = from user in testData
              group user.Attendance.Sum(a => a.presence) by user.UserId into result
              select "User" + result.Key + " - " + result.First();

foreach (var result in results)
{
    System.Console.WriteLine(result);
}

either of which you should be able to make the necessary adjustments to in order to get what you are seeking.

Edit: You should put the logic above in a method in the model and reference it in the controller, using it to create a typed view or partial view of type IEnumerable<string> . To do this, you'll remove the foreach loop and instead return results from the method.

However, you might find it more flexible, depending on your needs, to create a view or partial view typed as IEnumerable<UserCourse> and then put the original logic directly in the View like this:

@model IEnumerable<UserCourse>

@{
    var results = from user in Model //Notice the reference to Model instead of testData
                  group user.Attendance.Sum(a => a.presence) by user.UserId into result
                  select "User" + result.Key + " - " + result.First();
}

then elsewhere in the view you'll be able to reference the results variable and output it where necessary:

<div>
    @foreach (var result in results)
    {
        <p>@result</p>
    }
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM