如何使用 Linq 在 EF Code First 中执行 OFTYPE ONLY?
使用传统实体框架并使用 ESQL 进行查询时,您可以将 OFTYPE 与 ONLY 结合使用以仅返回基本类型。
请参阅:http://msdn.microsoft.com/en-us/library/bb399295 .aspx
在 Entity Framework Code First 中,我设置了继承,其中 B 是 A 的子类型。 MyContext.Set().OfType()
仍然返回 B 类型的元素。理想情况下,我想调用 MyContext.Set
().OfOnlyType
A>()
并且它的转换方式与使用 ESQL 的 OFTYPE ONLY 时相同。
我还发现我可以在 where 语句中使用 is 运算符,但同样,这将返回 A 和 B 实体。
如何编写仅过滤 A 类型元素的 linq 表达式?
When using traditional Entity Framework and querying with ESQL, you can use OFTYPE with ONLY to only return base types.
See: http://msdn.microsoft.com/en-us/library/bb399295.aspx
In Entity Framework Code First, I have inheritance set up where B is a subtype of A. Performing MyContext.Set<A>().OfType<A>()
still returns elements of type B. Ideally, I would like to call MyContext.Set<A>().OfOnlyType<A>()
and it would translate the same way as when using ESQL's OFTYPE ONLY.
I also found I can use the is
operator in a where statement, but again, that will return both A and B entities.
How can I write a linq expression that will filter to only elements of type A?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非以某种方式从结果集中排除派生实例,否则无法在 Linq-to-entities 中直接获取
A
实例(相当于ONLY
)。例如:There is no way to get only
A
instances directly (equivalent toONLY
) in Linq-to-entities unless you somehow exclude derived instances from the result set. For example: