可以将 IQueryable转换为到 IQueryable ?
我了解协方差,并且我知道一般来说,在 v4.0 之前,它在 C# 中是不可能的。
不过我想知道一个具体案例。是否有某种方法可以通过某种方式创建一个包装类,将 IQueryable
转换为 IQueryable
,该包装类实际上并不执行查询,但实际上可以“传递” " .Where<>()
调用?
我的用例是我正在尝试处理具有许多相似表的数据库模式。大部分字段是公共的,很多公共字段需要在每个表上查询。我正在使用 LinqToSql。我希望避免重复每个表的所有查询。
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 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?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是您正在寻找的吗?
OfType 方法将收集指定类型的所有内容,如果集合中的所有项目都是 Base 类型或派生自 Base 类型,则它们应该符合条件。在接下来的地方,所有项目都是 Base 类型。
Is the following what you are looking for?
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: