这是我模型的相关部分:
这里是我的模型类的代码:
/// <summary>
/// Retrieves a list of vans for binding with the BeachReach Snapshot
/// grid. Note: This method uses a POCO class that does not participate
/// in management by the entity context. See the DisplayVan class for more info
/// </summary>
/// <param name="setVanIDs">
/// You may limit which vans will be returned by passing a hash
/// set of their van IDs here
/// </param>
/// <returns>List of type DisplayVan - Unmanaged</returns>
public static List<DisplayVan> GetVansForSnapshot(HashSet<int> setVanIDs = null) {
using (BeachReachDataEntities objContext = new BeachReachDataEntities()) {
var qryVans = from v in objContext.vans
.Include("school")
.Include("location")
select new DisplayVan {
vanID = v.vanID,
vanName = v.vanName,
phone = v.phone,
capacity = (int)v.capacity,
schoolName = v.school.schoolName,
lastLocationName = v.location.locationName,
statusNote = v.statusNote,
isOffline = (v.isOffline == 1) ? true : false,
isPrayerRoom = (v.isPrayerRoom == 1) ? true : false,
isNotReady = (v.isNotReady == 1) ? true : false,
creationDate = v.creationDate,
modifiedDate = v.modifiedDate,
vanAssignments = (from va in objContext.vanAssignments
from pr in objContext.pickupRequests
where va.vanID == v.vanID
where pr.pickupRequestID == va.pickupRequestID
select va)
};
if (setVanIDs != null && setVanIDs.Count > 0)
return qryVans.Where(v => setVanIDs.Contains(v.vanID)).ToList<DisplayVan>();
else
return qryVans.ToList<DisplayVan>();
}
}
// --------------------------------------------------------
我也尝试过:
var qryVans = from v in objContext.vans
.Include("school")
.Include("location")
.Include("vanAssignments")
.Include("vanAssignments.pickupRequest")
select new DisplayVan {
vanID = v.vanID,
vanName = v.vanName,
phone = v.phone,
capacity = (int)v.capacity,
schoolName = v.school.schoolName,
lastLocationName = v.location.locationName,
statusNote = v.statusNote,
isOffline = (v.isOffline == 1) ? true : false,
isPrayerRoom = (v.isPrayerRoom == 1) ? true : false,
isNotReady = (v.isNotReady == 1) ? true : false,
creationDate = v.creationDate,
modifiedDate = v.modifiedDate,
vanAssignments = v.vanAssignments
};
忽略is ****属性。我正在使用不支持boolean
类型的MySQL数据库。
查询必须投射到DisplayVan
而不是van
实体,因为我有一些我绑定到数据网格的计算属性。
问题是当我尝试访问pickupRequest
实体的vanAssignment
导航属性时。无论我做什么,pickupRequest
属性都是null
。注意,我确实有懒加载。如何通过上面的查询加载vanAssignment的pickupRequest关系?
最后一点:vanAssignments
属性是类型vanAssignment
的列表。感谢您提供任何有关此问题的见解。
修改
另外一个问题。所以从你的例子中,我想去vanAssignment.person.firstName。我试过这个:
var qryVans = (from v in objContext.vans
select new {
DisplayVan = new DisplayVan {
vanID = v.vanID,
vanName = v.vanName,
phone = v.phone,
capacity = (int)v.capacity,
schoolName = v.school.schoolName,
lastLocationName = v.location.locationName,
statusNote = v.statusNote,
isOffline = (v.isOffline == 1) ? true : false,
isPrayerRoom = (v.isPrayerRoom == 1) ? true : false,
isNotReady = (v.isNotReady == 1) ? true : false,
creationDate = v.creationDate,
modifiedDate = v.modifiedDate,
vanAssignments = v.vanAssignments
},
PickupRequests = v.vanAssignments.Select(va => va.pickupRequest),
People = v.vanAssignments.Select(va => va.pickupRequest.person)
}).ToList().Select(e => e.DisplayVan);
当我尝试访问该属性时,出现以下错误:
答案 0 :(得分:5)
这应该有效:
var qryVans =
(from v in objContext.vans
select new
{
DisplayVan = new DisplayVan
{
vanID = v.vanID,
vanName = v.vanName,
phone = v.phone,
capacity = (int)v.capacity,
schoolName = v.school.schoolName,
lastLocationName = v.location.locationName,
statusNote = v.statusNote,
isOffline = (v.isOffline == 1) ? true : false,
isPrayerRoom = (v.isPrayerRoom == 1) ? true : false,
isNotReady = (v.isNotReady == 1) ? true : false,
creationDate = v.creationDate,
modifiedDate = v.modifiedDate,
vanAssignments = v.vanAssignments
},
PickupRequests = v.vanAssignments.Select(va => new {PickupReuqest = va.pickupRequest, Person = va.pickupRequest.Person})
}
).ToList().Select(e => e.DisplayVan);
在此构造中忽略包含,因此您应该保留它们。您应该使用其他属性指定您也想要pickupRequests。 ToList()
执行获取所需数据的查询,并且由于所有必需的pickupRequests都在上下文中,因此您将填充正确的字段。 EF分析您在视图模型中请求的字段并准备sql语句,这将占用它们。