1

我有一种方法可以循环记录并在每条记录上执行选择。我想要一些指导,以便将所有查询汇总并在一次往返中发送到数据库。我知道 CreateMultiQuery() 和 CreateMultiCriteria() 方法,但我不确定如何在下面的当前代码中实现?

谢谢

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'
4

1 回答 1

0

也许你应该给 RollAttendeeUpdater 整套要更新的卷 - 然后它有足够的知识来同时为所有卷做一些 GetByRoll() 的变体。

或者,让调用者完成查询并传入卷和注册列表,而不是传递存储库实例。

于 2012-10-11T07:25:42.563 回答