我如何可以转换的DBQuery< T>到的ObjectQuery< T>? [英] How can i convert a DBQuery<T> to an ObjectQuery<T>?

查看:253
本文介绍了我如何可以转换的DBQuery< T>到的ObjectQuery< T>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的DBQuery< T> 其转换为的IQueryable< T> (此位的作品精细)。但后来我想给的IQueryable转换为的ObjectQuery ..这失败: -

I've got a DBQuery<T> which converts to an IQueryable<T> (this bit works fine). But then I'm trying to convert the IQueryable to an ObjectQuery .. which fails :-

public void Foo(this IQueryable<T> source)
{
    // ... snip ...

    ObjectQuery<T> objectQuery = source as ObjectQuery<T>;
    if (objectQuery != null)
    {
        // ... do stuff ...
    }
}

这曾经工作之前,我改变了为实体框架4 CTP5的魔术独角兽等等等等的。现在,它不工作 - 即。 的ObjectQuery

This used to work before I changed over to Entity-Framework 4 CTP5 Magic Unicorn blah blah blah. Now, it's not working - ie. objectQuery is null.

现在,的DBQuery&LT; T&GT;继承的IQueryable&LT; T&GT; ..所以我想这应该工作

Now, DBQuery<T> inherits IQueryable<T> .. so I thought this should work.

如果我改变code到..

If i change the code to ..

var x = (ObjectQuery<T>) source;

那么下面的异常被抛出: -

then the following exception is thrown :-

System.InvalidCastException:无法   类型强制转换的对象   System.Data.Entity.Infrastructure.DbQuery 1 [Tests.Models.Order]   输入   System.Data.Objects.ObjectQuery 1 [Tests.Models.Order]。

System.InvalidCastException: Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery1[Tests.Models.Order]' to type 'System.Data.Objects.ObjectQuery1[Tests.Models.Order]'.

有什么建议?

推荐答案

的DBQuery&LT; T&GT; 包含包含方法这样你就不会需要转换为的ObjectQuery 的ObjectQuery 是不是从的DBQuery 访问实例 - 它被包裹在内部类型 InternalQuery 和转换操作符是不确定的。

DbQuery<T> contains Include method so you don't need to convert to ObjectQuery. ObjectQuery is not accessible from DbQuery instance - it is wrapped in internal type InternalQuery and conversion operator is not defined.

顺便说一句。当您添加使用System.Data.Entity的和refrence CTP5你就可以调用包含的IQueryable&LT; T&GT;!

Btw. when you add using System.Data.Entity and refrence CTP5 you will be able to call Include on IQueryable<T>!

这篇关于我如何可以转换的DBQuery&LT; T&GT;到的ObjectQuery&LT; T&GT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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