在poco中使用子查询来填充属性
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能在 linq-to-entities 查询中使用自定义属性。只能使用直接映射到数据库的属性 = 您必须在返回
ArrivalStation
的 linq-toentities 查询中直接使用子查询。也许它可以包装为简单的扩展方法:其中
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:Where
Route
is your principal entity where way points are defined.FirstOrDefault
is used because sub queries cannot use justFirst
.