可能的IQueryable&LT转换,衍生的GT;到IQueryable的<基地以及GT ;? [英] Possible to convert IQueryable<Derived> to IQueryable<Base>?

查看:112
本文介绍了可能的IQueryable&LT转换,衍生的GT;到IQueryable的<基地以及GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是知道的协方差,我也知道,一般不会有可能在C#中,直到4.0版。

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

不过,我想了解一下具体情况。有没有得到转换的一些方法的IQueryable<衍生> 的IQueryable<基地> 通过某种方式创建一个包装类,不没有实际执行查询,但是实际上可以通过一个。凡<>()致电

However I am wondering about a specific case. Is there some way of getting converting IQueryable<Derived> to IQueryable<Base> by somehow creating a wrapper class that does not actually perform a query, but can actually "pass through" a .Where<>() call?

我的用例是,我想处理有许多相似的表的数据库模式。大多数字段是在常见的,许多常见的领域需要对每个表进行查询。我使用LinqToSql。我希望避免重复所有查询每个表。

My use case is that I am trying to deal with a database schema that has many similar tables. Most of the fields are in common, and many of the common fields need to be queried on each table. I'm using LinqToSql. I was hoping to avoid duplicating all the queries for each table.

推荐答案

是你在找什么下面?

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



OfType方法将收集起来什么,是指定的类型,如果集合中的所有项目是任一类型的相应的,或从型基衍生,它们应该出线。在随后的地方,所有项目将型基地。

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.

这篇关于可能的IQueryable&LT转换,衍生的GT;到IQueryable的&LT;基地以及GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆