这是我模型的相关部分:

这里是我的模型类中的代码:

/// <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 实体,因为我有一些我绑定(bind)到数据网格的计算属性。

问题是当我尝试访问 pickupRequest 实体的 vanAssignment 导航属性时。无论我做什么, pickupRequest 属性都是 null 。请注意,我确实有延迟加载。如何通过上面的查询加载 vanAssignment 的 PickRequest 关系?

最后一点: 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);

当我尝试访问该属性时,出现以下错误:

最佳答案

这应该有效:

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 分析您在 View 模型中请求的字段并准备将接受它们的 sql 语句。

关于c# - LINQ 到实体 : Loading One-To-Many Navigation Properties in Strong Typed Projections,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5012273/

10-12 03:58