我有一個循環瀏覽記錄並在每條記錄上執行選擇的方法。 我想要一些指導,以便所有查詢都彙總並在一次往返中發送到數據庫。 我知道CreateMultiQuery()和CreateMultiCriteria()方法,但我不確定如何在下面的當前代碼中實現?太多的數據庫調用 - NHibernate
感謝
foreach (Roll roll in attendanceRegisterRolls.Items)
{
RollAttendeeUpdater attendeeUpdater = new RollAttendeeUpdater(enrolmentRepository, roll);
attendeeUpdater.**AddNewStudentEnrolmentsAsAttendees();**
attendeeUpdater.RefreshEffectiveAttendance(register);
}
**Constructor**
public RollAttendeeUpdater(IEnrolmentRepository enrolmentRepository, Roll roll)
{
_roll = roll;
_enrolments = enrolmentRepository.GetByRoll(_roll);
}
public interface IEnrolmentRepository : IRepository<Enrolment>
{
IEnumerable<Enrolment> GetByRoll(Roll roll);
bool IsStudyCompleted(string resultCode);
}
public IEnumerable<Enrolment> GetByRoll(Roll roll)
{
var query = _session.CreateQuery(
@"select e
from Enrolment e
join fetch e.Student
where e.Roll.Id.RollNumber = :rollNumber and e.Roll.Id.Year = :year
order by e.EnrolmentStatus");
query.SetString("rollNumber", roll.Id.RollNumber);
query.SetString("year", roll.Id.YearAsTwoDigitString);
return query.List<Enrolment>();
}
public void **AddNewStudentEnrolmentsAsAttendees()**
{
foreach (Enrolment enrolment in _enrolments)
{
Student student = enrolment.Student;
if (student.IsActive)
if (_roll.HasAttendee(student.Id) == false)
_roll.AddAttendee(student, new DateTimeRange(enrolment.StudyStartDate, enrolment.StudyEndDate));
}
}
public virtual bool HasAttendee(string studentId)
{
return _attendees.Any(a => a.Student.Id == studentId);
}
的AddNewStudentEnrolmentsAsAttendees()和enrolmentRepository.GetByRoll(_roll)方法返回的SQL語句:
select
enrolment0_.id_enrolment as id1_1_0_,
enrolledst1_.id_student as id1_10_1_,
enrolment0_.study_start_date as study2_1_0_,
enrolment0_.study_end_date as study3_1_0_,
enrolment0_.id_eft as id4_1_0_,
enrolment0_.enrolment_date as enrolment5_1_0_,
enrolment0_.enrolment_start_date as enrolment6_1_0_,
enrolment0_.enrolment_status as enrolment7_1_0_,
enrolment0_.fee_band as fee8_1_0_,
enrolment0_.id_course as id9_1_0_,
enrolment0_.limitation_date as limitation10_1_0_,
enrolment0_.result_date as result11_1_0_,
enrolment0_.roll_number as roll12_1_0_,
enrolment0_.year as year1_0_,
enrolment0_.id_student as id14_1_0_,
enrolment0_.result_code as result15_1_0_,
enrolledst1_.surname as surname10_1_,
enrolledst1_.given_names as given3_10_1_,
enrolledst1_.preferred_name as preferred4_10_1_,
enrolledst1_.title as title10_1_,
enrolledst1_.gender as gender10_1_,
enrolledst1_.tafe_international_id as tafe7_10_1_,
enrolledst1_.active_ind as active8_10_1_,
enrolledst1_.date_of_birth as date9_10_1_
from dt_modular_enrolment enrolment0_ inner join dt_student enrolledst1_ on enrolment0_.id_student=enrolledst1_.id_student
where (
enrolment0_.roll_number=?)
and
(
enrolment0_.year=?)
order by enrolment0_.enrolment_status; p0 = 'AH0130',
p1 = '12'
SELECT
attendees0_.roll_number as roll5_1_,
attendees0_.year as year1_,
attendees0_.id_class_attendee as id1_1_,
attendees0_.id_class_attendee as id1_12_0_,
attendees0_.attendance_start_date as attendance2_12_0_,
attendees0_.attendance_end_date as attendance3_12_0_,
attendees0_.enrolled_ind as enrolled4_12_0_,
attendees0_.roll_number as roll5_12_0_,
attendees0_.year as year12_0_,
attendees0_.prospective_ind as prospect7_12_0_,
attendees0_.student_identification as student8_12_0_
FROM dt_class_attendee attendees0_
WHERE attendees0_.roll_number=? and
attendees0_.year=?; p0 = 'AH0130',
p1 = '12'
您當前發佈的代碼不包含任何NHibernate查詢。你能更新你的問題,並在下面的方法中顯示你的代碼的樣子:enrolmentRepository.GetByRoll()? –
爲什麼你在'RollAttendeeUpdater'中使用'enrolmentRepository'而不是在構造函數中給它註冊。然後,您可以輕鬆批量選擇 – Firo
@Randy - 上面顯示了GetByRoll()方法,它位於IEnrolmentRepository接口中。我認爲sql是在AddNewStudentEnrolmentsAsAttendees()上創建的?如果任何人可以張貼他們的想法樣本,我將不勝感激 - 謝謝。 – Darren