连接多个表与where条件

时间:2016-07-16 02:42:47

标签: c# asp.net linq

我在这里有一些问题。这是显示所有会议详细信息的代码。现在我想再添加一个条件,即,其中ar.Response ==" ConfirmedAtVenue" 此响应属性将存储在AttendeeResponse表中。我无法使它工作,我总是得到一个错误或null异常。我试图放入最后一行甚至是加入j2之后,但仍然没有工作。

     private MeetingIndexViewmodel GetMeetingDetailsForIndexView(int id)
    {
        //Names
        string AttendeeNameMatch = GetAttendeeName(id);
        string ConvenerNameMatch = GetConvenerName(id);
        string OrganiserNameMatch = GetOrganiserName(id);
        //Telephone Numbers
        int AttendeeTelNoMatch = GetAttendeeTelNo(id);
        int ConvenerTelNoMatch = GetConvenerTelNo(id);
        int OrganiserTelNoMatch = GetOrganiserTelNo(id);
        //Email
        string AttendeeEmailMatch = GetAttendeeEmail(id);
        string ConvenerEmailMatch = GetConvenerEmail(id);
        string OrganiserEmailMatch = GetOrganiserEmail(id);

        var meetingDetails = (from p in db.Persons
                             join ts in db.Timeslots on p.PersonID equals ts.PersonID into j1
                             from ts in j1.DefaultIfEmpty()
                             join ar in db.AttendeeResponses on ts.TimeslotID equals ar.TimeslotID into j2
                             from ar in j2.DefaultIfEmpty()
                             join pm in db.PersonMeetings on ar.PersonMeetingID equals pm.PersonMeetingID into j3
                             from pm in j3.DefaultIfEmpty()
                             join m in db.Meetings on pm.MeetingID equals m.MeetingID into j4
                             from i in j4.DefaultIfEmpty()
                             where i.MeetingID == id && pm.MeetingRole.Equals("Attendee") //&& ar.Response.Equals("NoResponse")
                             //The above two lines were needed for the null MeetingIDs to be reported
                              select new MeetingIndexViewmodel
                              {
                                  MeetingID = id,
                                  AttendeeName = AttendeeNameMatch,
                                  ConvenerName = ConvenerNameMatch,
                                  OrganiserName = OrganiserNameMatch,
                                  StartTime = ts.StartTime.ToString() ?? "null",
                                  Duration = ts.Duration.ToString() ?? "null",
                                  AttendeeResponseID = ar.AttendeeResponseID,
                                  Response = ar.Response,
                              }
                              ).FirstOrDefault();
        return meetingDetails;
    }

2 个答案:

答案 0 :(得分:1)

根据您的评论,您似乎需要一个标准inner join,所以我认为这应该有效:

var meetingDetails = from p in db.Persons
                     join ts in db.Timeslots on p.PersonID equals ts.PersonID
                     join ar in db.AttendeeResponses on ts.TimeslotID equals ar.TimeslotID
                     join pm in db.PersonMeetings on ar.PersonMeetingID equals pm.PersonMeetingID
                     join m in db.Meetings on pm.MeetingID equals m.MeetingID
                     where m.MeetingID == id
                     && pm.MeetingRole == "Attendee"
                     && ar.Response == "ConfirmedAtVenue"
                     select new MeetingIndexViewmodel
                     {
                         MeetingID = m.MeetingID,
                         AttendeeName = AttendeeNameMatch,
                         ConvenerName = ConvenerNameMatch,
                         OrganiserName = OrganiserNameMatch,
                         StartTime = ts.StartTime != null ? ts.StartTime.ToString() : "null", // or null or string.Empty
                         Duration = ts.Duration.ToString(), // Use this approach if StartTime and Duration can't be null
                         AttendeeResponseID = ar.AttendeeResponseID,
                         Response = ar.Response
                     };

修改

由于这不起作用,你应该逐桌走,直到找到没有结果的东西。例如:

// Joining Persons and Timeslots
var details1 = from p in db.Persons
               join ts in db.Timeslots on p.PersonID equals ts.PersonID
               select p;

// Joining all tables
var details2 = from p in db.Persons
               join ts in db.Timeslots on p.PersonID equals ts.PersonID
               join ar in db.AttendeeResponses on ts.TimeslotID equals ar.TimeslotID
               join pm in db.PersonMeetings on ar.PersonMeetingID equals pm.PersonMeetingID
               join m in db.Meetings on pm.MeetingID equals m.MeetingID
               select p;

// Joining all tables and filtering
var details3 = from p in db.Persons
               join ts in db.Timeslots on p.PersonID equals ts.PersonID
               join ar in db.AttendeeResponses on ts.TimeslotID equals ar.TimeslotID
               join pm in db.PersonMeetings on ar.PersonMeetingID equals pm.PersonMeetingID
               join m in db.Meetings on pm.MeetingID equals m.MeetingID
               where m.MeetingID == id
               && pm.MeetingRole == "Attendee"
               && ar.Response == "ConfirmedAtVenue"
               select p;

您何时开始获取新的结果集?

答案 1 :(得分:0)

将您的where子句更新为以下内容。

where i.MeetingID == id && pm.MeetingRole.Equals("Attendee") && ar.Response == "ConfirmedAtVenue" && ar.Response != null