我首先使用实体框架 4.1 代码,这是我的域类的简化版本:
public class Tour
{
public string TourName { get; set; }
public virtual List<Event> Events { get; set; }
}
public class Event
{
public string EventName { get; set; }
public virtual List<Participant> Participants { get; set; }
}
public class Participant
{
public string ParticipantName { get; set; }
}
在客户端应用程序中,我使用这些类:
public class EventItem
{
public string DisplayName { get; set; }
public IEnumerable<ParticipantItem> Tourists { get; set; }
}
public class ParticipantItem
{
public string Name { get; set; }
}
和查询:
var query = from tour in context.Tours
from evt in tr.Events
where tour.ApiKey == APIKey
select new EventItem
{
DisplayName = evt.EventName,
Tourists = from person in evt.Participants
select new ParticipantItem
{
Name = person.ParticipantName
}
};
return query.ToList();
它的工作原理是内部查询表达式不返回任何数据 ( from person in evt.Participants
...)。我认为这与延迟加载 Event 对象的 Participants 属性有关。但是,如果这是问题,如何在此查询中使用“包含”?