IQueryable 到 IQueryable
是否可以将 IQueryable 对象转换为 IQueryable,其中 T 是映射实体? (T 将是 POCO 类)。
提前致谢。
is it possibile convert an IQueryable object to IQueryable where T is a mapped entity? (T will be a POCO class).
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需
Cast()
即可。假设它是相同类型的可查询。否则,您可以使用OfType()
< /a> 过滤方法,过滤掉某种类型的项目。然而,在您的情况下,您说您正在使用 Dynamic LINQ 并进行动态投影。考虑这个完全组成的查询:
它产生
IQueryable
类型的查询。毕竟,您不能将其转换为特定类型IQueryable
的查询,T
是什么? Dynamic LINQ 库的作用是创建一个派生自DynamicCass
的类型。您可以强制转换为IQueryable
(query.Cast()
),但您将无权访问这些属性,因此它没有实际意义。实际上,在这种情况下,您拥有的唯一不错的选择是使用
dynamic
来访问这些属性。如果要将其转换为 POCO 对象的查询,则必须使用 LINQ to Objects 作为单独的步骤进行转换。
如果您必须有一个
IQueryable
,那么您首先就不应该使用动态投影。鉴于强制转换对于 LINQ to Entities 不起作用,那么我想获得 POCO 对象的强类型集合的唯一选择是将其分解为循环。
Just
Cast<T>()
it. Assuming it is a queryable of the same type. Otherwise you could use theOfType<T>()
filtering method to filter out items of a certain type.However in your case, you say that you are using Dynamic LINQ and doing a dynamic projection. Consider this completely made up query:
It results in a query of type
IQueryable
. You cannot cast this to a query of a specific typeIQueryable<T>
after all, what isT
? What the Dynamic LINQ library does is creates a type that derives fromDynamicCass
. You could cast toIQueryable<DynamicClass>
(query.Cast<DynamicClass>()
) but you will not have access to the properties so it's moot.Really the only nice option you have is to use
dynamic
to access these properties in this case.If you want to convert this to a query of your POCO objects, you'll have to do the conversion as a separate step but using LINQ to Objects.
If you must have an
IQueryable<T>
, then you should not use dynamic projections in the first place.Seeing as how the cast is not working for LINQ to Entities, then I suppose the only option you have to get a strongly type collection of your POCO objects is to break this out into a loop.