我正在尝试使用LINQ查询从我们的CRM在线环境中检索所有约会(我是编程的新手)。获取约会数据很容易,但我也想检索约会的必需参加者(这可能是一个帐户,例如联系),并从与会者那里获取一些元数据(例如姓名,电子邮件)。不幸的是,似乎不可能完成这项工作,并希望有人可以帮助我。
public AppointmentData[] RetrieveActivities(bool persistChange)
{
var appointmentData = new List<AppointmentData>();
using (var context = new FmServiceContext(_service))
{
var appointments = (from app in context.AppointmentSet
join a in context.AccountSet on app.Account_Appointments.AccountId equals a.AccountId
where app.StateCode != 0
select new {app, a});
foreach (var apappointments in appointments)
{
appointmentData.Add(new AppointmentData
{
//this should be the list of required attendees
RequiredAttendees = new ActivityParty[]
{
Attendeename = apappointments.a.Name
},
//Appointment data
AppointmentType = apappointments.app.fm_Typeafspraak == null ? null : DataHelper.GetOptionSetValueLabel(apappointments.app.LogicalName, "fm_typeafspraak", apappointments.app.fm_Typeafspraak.Value, _service),
Subject = apappointments.app.Subject,
StartDate = apappointments.app.ScheduledStart,
EndDate = apappointments.app.ScheduledEnd,
Duration = apappointments.app.ScheduledDurationMinutes,
Location = apappointments.app.Location,
Description = apappointments.app.Description,
Priority = apappointments.app.PriorityCode == null ? null : DataHelper.GetOptionSetValueLabel(apappointments.app.LogicalName, "prioritycode", apappointments.app.PriorityCode.Value, _service),
Status = apappointments.app.StateCode.ToString()
});
}
}
return appointmentData.ToArray();
}
答案 0 :(得分:0)
我认为您不需要加入,因为您的查询中已经存在活动方ID。您可能在这里遇到了加入功能的限制。这是一种方法:
foreach(var app in appointments)
{
var requiredAttendees = app.RequiredAttendees.ToList();
foreach(var ra in requiredAttendees)
{
// do whatever you want with each required attendee here, perform a separate retrieve for more details
}
}
此外,我认识到这是一个额外的步骤。如果您想尝试让您的加入工作,我建议您反对派对。如果你要去那条路线,你可能需要一个嵌套查询或更复杂的连接,因为与ActivityParty的关系是1:N。如果您只关心第一位参加者,请查看此链接:https://www.periscopedata.com/blog/4-ways-to-join-only-the-first-row-in-sql.html
答案 1 :(得分:0)
解决了!我确实使用了Brendon的方法来完成它。首先,我查询了所有的约会,
这是我的代码:
public AppointmentData[] RetrieveAppointments(bool persistChange)
{
var appointmentData = new List<AppointmentData>();
using (var context = new FmServiceContext(_service))
{
//First we get all relevant appointments
var appointments = (from app in context.AppointmentSet
where app.StateCode != 0
select new { app});
foreach (var appointment in appointments)
{
//we loop all the returned attendees of the appointments
var attendees = new List<Attendee>();
foreach (var attendee in appointment.app.RequiredAttendees)
{
if (attendee.PartyId != null)
{
//if an attendee is an account
if (attendee.PartyId.LogicalName == "account")
{
var account = (from acc in context.AccountSet
where acc.AccountId == attendee.PartyId.Id
select new {acc});
}
//add the attendee
{
attendees.Add(new Attendee
{
// get additional metadata of the attendee
});
}
appointmentData.Add(new AppointmentData
{
Attendees = attendees,
// get additional metadata of the appointment
});
}
}
}
return appointmentData.ToArray();
}
}
}
结果:约会列表,其中包含该约会所需的与会者列表。