开发者

Using subquery in poco to fill property

开发者 https://www.devze.com 2023-03-27 18:31 出处:网络
I am trying to use a property on a POCO that uses LINQ to ENTITY to pull the first object out of a HashSet property on the same POCO.My object contains the following:

I am trying to use a property on a POCO that uses LINQ to ENTITY to pull the first object out of a HashSet property on the same POCO. My object contains the following:

public virtual HashSet<ScheduleWaypoint> ScheduleWaypoints { get; set; }

public ScheduleWaypoint ArrivalStation {
        get {
            if (this.ScheduleWaypoints != null && this.ScheduleWaypoints.Count() > 0) {
                return this.ScheduleWaypoints.Where(row => row.WaypointType.Type.Trim() == "SA").OrderByDescending(row => row.ScheduledTime).First();
            } else
                return null;
        }
    }

If I were working with just 开发者_如何学JAVAone object I can't say for certain if this would work but I know that it does not work inside other linq queries. I don't have access to the ID of the ScheduleWaypoint when creating the object, only after it is populated could I possibly do that. Is there a way that I can get this to work? Right now it is telling me:

The specified type member 'ArivalStation' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

Is there something I can do to get access to this information on a property rather than constantly doing joins when I need the info?

Thanks.


You cannot use custom properties in linq-to-entities query. Only properties mapped directly to the database can be used = you must have sub query directly in your linq-to-entities query returning your ArrivalStation. Perhaps it can be wrapped as simple extension method:

public static IQueryable<ScheduleWaypoint> GetArrivalStation(this IQueryable<ScheduleWaypoints> waypoints, int routeId)
{
    return waypoints.Where(w => w.WaypointType.Type.Trim() == "SA" && w.Route.Id == routeId)
                    .OrderByDescending(w => w.ScheduledTime)
                    .FirstOrDefault();
}

Where Route is your principal entity where way points are defined. FirstOrDefault is used because sub queries cannot use just First.

0

精彩评论

暂无评论...
验证码 换一张
取 消