Possible to convert IQueryable<Derived> to IQueryable<Base>?

大兔子大兔子 提交于 2019-12-01 11:22:24

Is the following what you are looking for?

var results = queryable.OfType<Base>.Where(...);

The OfType method will gather up anything that is of the specified type, and if all items in the collection are either of type Base, or derived from type base, they should qualify. In the subsequent where, all items would be of type Base.

The following will also work, altough it is less pretty:

var results = queryable.Select(derived => (Base)derived).Where(...);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!