如何返回一个IQueryable&LT;东西&GT;作为一个IQueryable&LT; ISomething&GT; [英] How to return an IQueryable<Something> as an IQueryable<ISomething>
问题描述
我有一个类的东西
实现 ISomething
。我如何转换/施放从的IQueryable&LT;东西&GT;
到的IQueryable&LT; ISomething&GT;
。当我尝试投,我能够编译,但是中投的结果总是NULL。
I have a class Something
that implements ISomething
. How can I convert/cast from an IQueryable<Something>
to an IQueryable<ISomething>
. When I try to cast, I am able to compile, but the result of the cast is always NULL.
背景:我这样做的原因是因为我的的东西
类是codeSmith生成的类(PLINQO模板),有映射表装饰,方法,等我的 ISomething
界面POCO风格,因为它是愚蠢的,只是反映了东西的属性
类。它也存在于它自己的命名空间。我这样做,所以我不必引用持有的东西
类从我的服务层的组件。即持有东西组装
类只需要在我的仓库层被引用了。
Background: The reason I am doing this is because my Something
class is a CodeSmith-generated class (PLINQO template) that has table mapping decoration, methods, etc. My ISomething
interface is "POCO-style", in that it is dumb and simply mirrors the properties of the Something
class. It also exists in it's own namespace. I am doing this so I don't have to reference the assembly that holds the Something
class from my service layer. The assembly that holds the Something
class will only need to be referenced in my repository layer now.
推荐答案
您可以使用LINQ方法Cast
You can use LINQ method Cast
IQueryable<Something> something = ...
IQueryable<ISomething> casted = something.Cast<ISomething>();
这篇关于如何返回一个IQueryable&LT;东西&GT;作为一个IQueryable&LT; ISomething&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!