linq将两个表的结果组合成一个选择新的语句?

时间:2013-11-13 11:51:44

标签: linq entity-framework

使用以下查询如何更改我在select new中没有两组字段我希望信息进入一组没有两个的列和一个类型字段来说明它是一个traineeevent还是一个cpd事件?

    List<EmployeeCPDReportRecord> employeeCPDRecords = new List<EmployeeCPDReportRecord>();
    string employeeName;
    var q =  from cpd in pamsEntities.EmployeeCPDs
             from traineeEvent in pamsEntities.TrainingEventTrainees
             join Employee e in pamsEntities.Employees on cpd.EmployeeID equals e.emp_no
             join TrainingEventPart tEventPart in pamsEntities.TrainingEventParts on traineeEvent.TrainingEventPartId equals tEventPart.RecordId
             where (cpd.EmployeeID == id) && (startDate >= cpd.StartDate && endDate <= cpd.EndDate) &&
                    (traineeEvent.EmployeeId == id)
                    && (traineeEvent.TraineeStatus == 1 || traineeEvent.TraineeStatus == 2)
                    && (tEventPart.CPDHours > 0 || tEventPart.CPDPoints > 0)
                    && (cpd.CPDHours > 0 || cpd.CPDPoints > 0)
                   || traineeEvent.StartDate >= startDate
                    || traineeEvent.EndDate <= endDate

              orderby cpd.StartDate



             select new
                {
                    surname = e.surname,
                    forname1 = e.forename1,
                    forname2 = e.forename2,
                    EmployeeID = cpd.EmployeeID,
                    StartDate = cpd.StartDate,
                    EndDate = cpd.EndDate,
                    CPDHours = cpd.CPDHours,
                    CPDPoints = cpd.CPDPoints,
                    Description = cpd.Description,                            
                    TrainingStartDate = tEventPart.StartDate,
                    TrainingEndDate = tEventPart.EndDate,
                    TrainingCPDHours = tEventPart.CPDHours,
                    TrainingCPDPoints = tEventPart.CPDPoints,
                    TrainingEventDescription = tEventPart.Description

                };


    if (q != null)
        {
            Array.ForEach(q.ToArray(), i =>
            {

                   if (ContextBase.encryptionEnabled)
                    employeeName = ContextBase.Decrypt(i.surname) + ", " + ContextBase.Decrypt(i.forname1) + " " + ContextBase.Decrypt(i.forname2);
                else
                    employeeName = i.surname + ", " + i.forname1 + " " + i.forname2;

               if (i.TrainingStartDate !=  new DateTime(1900, 1, 1))

                   employeeCPDRecords.Add(new EmployeeCPDReportRecord(employeeName, Convert.ToDateTime(i.StartDate), Convert.ToDateTime(i.EndDate), Convert.ToDecimal(i.CPDHours), Convert.ToDecimal(i.CPDPoints), i.Description,i.t,i.EndDate,Convert.ToDecimal(i.CPDHours),Convert.ToDecimal(i.CPDPoints),i.Description,"L&D"));
               else
                   employeeCPDRecords.Add(new EmployeeCPDReportRecord(employeeName, Convert.ToDateTime(i.StartDate), Convert.ToDateTime(i.EndDate), Convert.ToDecimal(i.CPDHours), Convert.ToDecimal(i.CPDPoints), i.Description, i.StartDate, i.EndDate, Convert.ToDecimal(i.CPDHours), Convert.ToDecimal(i.CPDPoints), i.Description, "Employee CPD"));



         });
        }

1 个答案:

答案 0 :(得分:0)

使用此代码

List<EmployeeCPDReportRecord> employeeCPDRecords = new List<EmployeeCPDReportRecord>();

var q = ( from cpd in pamsEntities.EmployeeCPDs
                     from traineeEvent in pamsEntities.TrainingEventTrainees
                     join Employee e in pamsEntities.Employees on cpd.EmployeeID equals e.emp_no
                     join TrainingEventPart tEventPart in pamsEntities.TrainingEventParts on traineeEvent.TrainingEventPartId equals tEventPart.RecordId
                     where (cpd.EmployeeID == id) && (startDate >= cpd.StartDate && endDate <= cpd.EndDate) &&
                            (traineeEvent.EmployeeId == id)
                            && (traineeEvent.TraineeStatus == 1 || traineeEvent.TraineeStatus == 2)
                            && (tEventPart.CPDHours > 0 || tEventPart.CPDPoints > 0)
                            && (cpd.CPDHours > 0 || cpd.CPDPoints > 0)
                           || traineeEvent.StartDate >= startDate
                            || traineeEvent.EndDate <= endDate

                      orderby cpd.StartDate
                     select new EmployeeCPDReportRecord
                        {
YourEmployeColumnName=(ContextBase.encryptionEnabled==true?ContextBase.Decrypt(e.surname) + ", " + ContextBase.Decrypt(e.forname1) + " " + ContextBase.Decrypt(e.forname2):e.surname + ", " + e.forname1 + " " + e.forname2), 
YourEmployeeCPDColumnName=(i.TrainingStartDate !=new DateTime(1900, 1, 1)?"L&D":"Employee CPD")    
                            surname = e.surname,
                            forname1 = e.forename1,
                            forname2 = e.forename2,
                            EmployeeID = cpd.EmployeeID,
                            StartDate = cpd.StartDate,
                            EndDate = cpd.EndDate,
                            CPDHours = cpd.CPDHours,
                            CPDPoints = cpd.CPDPoints,
                            Description = cpd.Description,                            
                            TrainingStartDate = tEventPart.StartDate,
                            TrainingEndDate = tEventPart.EndDate,
                            TrainingCPDHours = tEventPart.CPDHours,
                            TrainingCPDPoints = tEventPart.CPDPoints,
                            TrainingEventDescription = tEventPart.Description

                        }).ToList<EmployeeCPDReportRecord>();