Sunteți pe pagina 1din 5

var studentRepo = new StudentRepository(); int gradeParaleloId = Convert.ToInt32(cmbGradeParalelo.SelectedValue ); var students = studentRepo.

FindAllStudentsFromGradeParalelo(g radeParaleloId);

int year = DateTime.Now.Year; int month = Convert.ToInt32(cmbMes.SelectedIndex) + 1; AttendanceRepository attendanceRepo = new AttendanceRepository();

for (int i = 0; i < students.Count; i++) { for (int j = 1; j < dataGridView1.Columns.Count; j++) { DateTime date = new DateTime(year, month, j);

var student = students[i]; var attendance = attendanceRepo.FindAttendance(student.StudentI d, date);

if (attendance == null) { attendance = new Data.Repositories.Attendance(); attendanceRepo.Add(attendance); }

attendance.StudentId = student.StudentId; attendance.DateOfAttendance = date;

attendance.Attended = dataGridView1[j, i].Value.ToString(); } }

attendanceRepo.SaveChanges();

My main thorn in the side is that for each day in the month, I have to query the database, and see if a record already exists in the DB for that day and student. The upside is that I'm putting each attendance in the datacontext and only pushing the save at the very end as a transaction of sorts. So I'm not inserting on every day for every student. Imagine for a class of 40 students. That's an easy 40 * 30 = 1200 queries on the spot. :x Is there some way for me to streamline this? I'm more concerned about this line:
var attendance = attendanceRepo.FindAttendance(student.StudentI d, date);

c# database entity-framework

shareimprove this question

edited Apr 15 '11 at 12:28

asked Apr 15 '11 at 0:37

Only Bolivian Here 1264

1 40*30=1200, not 120 Snowbear Apr 15 '11 at 5:12

I thought I typed that...let me edit. :) Only Bolivian Here Apr 15 '11 at 1 Are you having any performance issues? Try to time how long that many '11 at 12:39

This just highlights one of the issues with the Repository pattern that does go. OJ. Apr 16 '11 at 22:19 feedback

2 Answers
activeoldestvotes

up

vote do particularly the inner one. For instance you could make your repo query wn vote
DateTime date = new DateTime(year, month, **day**);

First of all, minor beef. I'd reccommend that you change your index names to be more descriptive,

Much more readable. When an index has a name, name it. If you're having performance issues, try separating out the queries. In psuedocode:
foreach day in month get students_with_records from database

foreach student in the_class if student NOT in students_with_records add record

This method will put the search through the student records on the client...it should be faster, since you will only have one query per day rather than one query per day per student. You really should profile the app and see if it's an issue, though.
shareimprove this answer
answered Apr 16 '11 at 0:35

Michael K 2,032617

This approach is ok, so long as there isn't a LOT of students. Otherwise you'll be pulling in a lot of data which you aren't going to use at all. If the DB is on a remote server this could be a nightmare. OJ. Apr 16 '11 at 22:14 feedback up vote do wn vote

Couple of things: 1. Although I can't tell for sure, it looks as if you're storing attendance if the student is present. Your database will grow very quickly that way. If possible, you might want to consider

having a calendar table for the school year that designates school days. That way, you will only need to store if the student is absent or tardy. Present days will be assumed and can be calculated from the calendar table. 2. This is small, but the following code may not work either: int year = DateTime.Now.Year; Take, for example, a teacher that wants to go back and correct attendance in December. It looks as if the will actually be selecting for December 2011...not 2010.
shareimprove this answer
answered Apr 18 '11 at 2:55

ray023 1656

Regarding the second point, that's a deliberate decision. The administrator does not want anyone tampering with legacy data (year old+ data) at all. That information is read-only so to speak. Only Bolivian Here Apr 18 '11 at 3:49 Would Dec 20, 2010 be considered "year old+ data" on Jan 3, 2011? Levinaris Apr 18 '11 at 14:32 Was this post useful to you? Your Answer

S-ar putea să vă placă și