gpt4 book ai didi

c# - 使用where条件连接多个表

转载 作者:太空宇宙 更新时间:2023-11-03 12:42:28 24 4
gpt4 key购买 nike

我有一些问题。这是显示所有 session 详细信息的代码。现在我想再添加一个条件,即 where ar.Response == "ConfirmedAtVenue" 此响应属性将存储在 AttendeeResponse 表中。我无法让它工作,我总是得到一个错误或空异常。我曾尝试将其放在最后一行甚至在加入 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;
}

最佳答案

根据您的评论,您似乎需要一个标准的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;

您什么时候开始获得新的结果集合?

关于c# - 使用where条件连接多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38407206/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com