Possible to convert IQueryable to IQueryable<Base>?

前端 未结 2 490
遥遥无期
遥遥无期 2021-01-15 13:17

I know about covariance, and I know that in general it will not be possible in C# until v4.0.

However I am wondering about a specific case. Is there some way of g

相关标签:
2条回答
  • 2021-01-15 13:29

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

    var results = queryable.Select(derived => (Base)derived).Where(...);
    
    0 讨论(0)
  • 2021-01-15 13:44

    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.

    0 讨论(0)
提交回复
热议问题