Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 ...
    }
}

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.

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

If i change the code to ..

var x = (ObjectQuery<T>) source;

then the following exception is thrown :-

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]'.

Any suggestions?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
498 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...