12

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?

Taryn
  • 242,637
  • 56
  • 362
  • 405
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
  • 1
    Why do you need to cast it to ObjectQuery? – anon Jan 22 '11 at 04:33
  • I think you're going to need a method that takes a DBQuery and constructs an ObjectQuery. That or an explicit conversion. I realize either one of those is a pain, though. – Justin Morgan - On strike Jan 22 '11 at 04:52
  • 1
    This is an interesting question. DbQuery doesn't have a direct relationship to ObjectQuery, unlike DbContext vs ObjectContext. I doubt that a direct conversion is possible. Again, you should re-examine why you need the cast in the first place. Also, where is "source" coming from? – anon Jan 22 '11 at 05:13
  • I need to get access to the `Include` method, which an `IQueryable' does not BUT an `IObjectQuery` does. – Pure.Krome Jan 22 '11 at 10:10

3 Answers3

16

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>!

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • no way! wikid :) It sure does! In fact, int's in the CTP5 System.Data.Entiy.DbExtensions namespace! And the code is (more or less) exactly what I had for my EF 3.5 code. (I made my own Include extension methods of `ObjectSet` (er.. i think it was `ObjectSet` ..) Dude - THANKS SO MUCH! win win win ... – Pure.Krome Jan 22 '11 at 23:39
11

Using reflection, you can do this:

var dbQuery = ...;
var internalQueryField = dbQuery.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).FirstOrDefault(f => f.Name.Equals("_internalQuery"));
var internalQuery = internalQueryField.GetValue(dbQuery);
var objectQueryField = internalQuery.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).FirstOrDefault(f => f.Name.Equals("_objectQuery"));

// Here's your ObjectQuery!
var objectQuery = objectQueryField.GetValue(internalQuery) as ObjectQuery<T>;
Josh Mouch
  • 3,480
  • 1
  • 37
  • 34
0

Not sure what you're trying to do with it, but would a dynamic variable help?

Using Type dynamic (C# Programming Guide)

Justin Morgan - On strike
  • 30,035
  • 12
  • 80
  • 104