如何使用 Linq 在 EF Code First 中执行 OFTYPE ONLY?

发布于 2024-12-21 18:28:30 字数 563 浏览 1 评论 0原文

使用传统实体框架并使用 ESQL 进行查询时,您可以将 OFTYPE 与 ONLY 结合使用以仅返回基本类型。

请参阅:http://msdn.microsoft.com/en-us/library/bb399295 .aspx

在 Entity Framework Code First 中,我设置了继承,其中 B 是 A 的子类型。 MyContext.Set().OfType() 仍然返回 B 类型的元素。理想情况下,我想调用 MyContext.Set().OfOnlyTypeA>() 并且它的转换方式与使用 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 技术交流群。

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

发布评论

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

评论(1

旧伤还要旧人安 2024-12-28 18:28:30

除非以某种方式从结果集中排除派生实例,否则无法在 Linq-to-entities 中直接获取 A 实例(相当于 ONLY)。例如:

var list = MyContext.Set<A>().Where(a => !(a is B)).ToList();

There is no way to get only A instances directly (equivalent to ONLY) in Linq-to-entities unless you somehow exclude derived instances from the result set. For example:

var list = MyContext.Set<A>().Where(a => !(a is B)).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文