我首先使用Entity framework 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属性有关。但如果问题是如何在此查询中使用“包含”?