LINQ to Entities: loading one-to-many navigation properties in strong typed views

Here is the relevant part of my model:

enter image description here

And here is the code from my model class:

/// <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>();

    }

}

// --------------------------------------------------------

I also tried:

        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
                      };

Ignore **** properties. I am using a MySQL database that does not support types boolean.

A query must be designed on DisplayVaninstead of an object vanbecause I have some computable properties that I bind to a datagrid.

The problem is that I am trying to access the navigation attribute of an pickupRequestobject vanAssignment. No matter what I do, the property pickupRequest null. Notice I have lazy loading. How to load vanAssignment pickupRequest relation through my request above?

: vanAssignments vanAssignment. , .

Edit

. , , 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);

, :

enter image description here

+3
1

:

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, .

+5

Source: https://habr.com/ru/post/1792408/


All Articles