0

我因按月显示学生出勤视图而陷入困境。我正在尝试使用 linq 实现它。我想要的输出类似于

student ID | 1  |2  |3  |4  |5  |6  |7  |8  |9 |10 |................
1          | P  |P  |A  |L  |P  |A  |P  |L  |P |A  |................

我有两张桌子

学生桌

 public class StudentMeta
    {
        public int Student_ID { get; set; }
        public string Roll_No { get; set; }
        public string Name { get; set; }
        etc ......
}

和学生考勤表

  public partial class tbl_Students_Attandance
    {
        public int monthdays { get; set; }
    }



    public class StuAttandance
    {
        public int id { get; set; }
        public System.DateTime AttandanceDate { get; set; }
        public int StudentID { get; set; }
        public string Status { get; set; }
        public int ClassSectionId { get; set; }
    }

我将使用一个函数,在该函数中,我将通过使用 Jquery 从下拉列表中选择月份来传递月份

  public ActionResult ViewMonthlyAttandance(int monthDays, int classID)
    {
        ViewBag.ClassesList = new SelectList(db.tbl_Classes, "Id", "Class_Name");

      List<tbl_Students_Attandance> Stu_Attendence =  db.tbl_Students_Attandance.Where(x => x.ClassSectionId == classID).ToList();

        // here some "Group by" statment will be used to get my "P" or "A" Stu_Staus from db according to student_ID


        return View();
    }

我知道我的视图会是这样的

<table>
    <thead>
        <tr>
            <th>Attendance Day</th>
            @foreach (var MD in Model.MonthDays)
            {
                <th>@MD</th>
            }

        </tr>
    <thead>
    <tbody>
        @foreach (var student in Model.Students)
        {
            <tr>
                <td>@student.Name</td>
                @foreach (var item in Model.Stu_Staus)
                {
                    <td>@item</td>
                }
            </tr>
        }
    </tbody>
}

我只想知道如何将数据从控制器传递到视图,以及是否需要在视图中进行任何更改。

任何帮助,将不胜感激。感谢你。

4

1 回答 1

0

所以你有一个序列,StuAttendances其中每个都StuAttendance属于一个Student和一个ClassSection使用外键的位置。

每一个StuAttendance也有一个属性AttendanceDate和一个Status反映出勤率的属性AttendanceDate

现在给定 aclassId和 a monthId(从 1..12 开始编号的月份),您需要一个对象序列,其中每个对象都包含 a StudentId,并且List此列表中的每个元素都包含具有此索引的本月日期的学生状态具有请求的月份 ID 的月份

后者很难说,如果monthId = 7,则列表中的元素[14]是7月14日学生的出勤状态(quatorze juillet)

我在你的规格中遗漏了一些项目。

  • monthDays 是不够的,你还需要一年,表示你想要 2018 年的所有七月日期。而不是 2017 年的七月日期
  • 在您的数据库结构中,可以让学生在同一日期缺席和在场,或者在同一日期缺席多次。
  • 在您的数据库结构中,我们也有可能在 2018 年 7 月 14 日没有任何学生的出勤状态。如果是这种情况,您希望最终结果是什么?

首先,我们需要一系列 Datetime 对象,我们希望在这些对象上参与:

int year = ...
int month = ...
DateTime startOfMonth = new DateTime(year, month, 1);
DateTime startOfNextMonth = startOfMonth.AddMonths(+1);
int daysInMonthCount = (int)((startOfNextMonth - startOfMonth).TotalDays());

IEnumerable<DateTime> daysToInvestigate = Enumerable.Range(0, daysInMonthCount)
    .Select(day => startOfMonth.AddDays(day));

现在我们已经得到了最终结果中想要的天数序列,我们可以让所有StuAttendances学生在classSectionid以下日子里参加daysToInvestigate

int classSectionId = ...
var studentsInClassDuringMonth = StuAttandance
    .Where(studentAttendance => studentAttence.ClassSectionId = classSectionId
         && studentAttendance => daysToInvestigate.Contains(studentAttendance.AttendanceDate);

按 StudentId 将它们全部分组,并且您在每个学生请求的月份内获得了出勤率:

var attendancyPerStudent = studentsInClassDuringMonth.GroupBy(student => student.StudentId)

每个组包含该班级一名学生在本月的所有数据。

由于本月某些日期可能没有学生记录,您不能说列表中的元素编号 [4] 包含第 5 天的状态。因此,最终结果中我们需要日期和状态:

var result = attendancyPerStudent.Select(group => new
{
     StudentId = group.Key,
     Attendancy = group.Select(attendancy => new
     {
         Date = attendancy.AttendanceDate,
         Status = attendany.Status,
     });
})

TODO:如果需要,可以做一个大的 LINQ 语句。由于尚未执行任何查询,因此不要期望任何性能改进。但是,预计可读性会降低,所以我不确定这是否明智。

于 2018-07-19T11:41:15.623 回答