在poco中使用子查询来填充属性

发布于 2024-11-29 14:04:57 字数 858 浏览 1 评论 0原文

我正在尝试使用 POCO 上的一个属性,该属性使用 LINQ to ENTITY 从同一 POCO 上的 HashSet 属性中提取第一个对象。我的对象包含以下内容:

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

如果我只使用一个对象,我不能肯定地说这是否有效,但我知道它在其他 linq 查询中不起作用。创建对象时我无权访问 ScheduleWaypoint 的 ID,只有在填充该对象后我才能执行此操作。有什么方法可以让它发挥作用吗?现在它正在告诉我:

LINQ to 不支持指定的类型成员“ArivalStation” 实体。仅初始值设定项、实体成员和实体导航 支持属性。

我可以做些什么来访问有关属性的此信息,而不是在需要信息时不断进行连接?

谢谢。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沙沙粒小 2024-12-06 14:04:57

您不能在 linq-to-entities 查询中使用自定义属性。只能使用直接映射到数据库的属性 = 您必须在返回 ArrivalStation 的 linq-toentities 查询中直接使用子查询。也许它可以包装为简单的扩展方法:

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

其中 Route 是定义路径点的主要实体。使用 FirstOrDefault 是因为子查询不能仅使用 First

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文