views:

372

answers:

4

I am using PredicateBuilder, which means that I am using the AsExpandable extension method. The problem is that I can no longer Trace my SQL queries as the following error is thrown when I try to cast the query to ObjectQuery so that I can do a ObjectQuery.ToTraceString() call on it...

Unable to cast object of type 'LinqKit.ExpandableQuery`1[Genesis.Person]' to type 'System.Data.Objects.ObjectQuery'.

Any Ideas?

+1  A: 

I'm not familiar with your precise problem but can you not run the trace on the SQL server instead using (eg profiler assuming MSSQL) ?

This would still allow you to see the executed queries and you could restrict it to be just your machine that is recorded...

Basiclife
+3  A: 

I would recommend running a Dump() on your query in LINQPad, and using the SQL tab to see what SQL was generated. Do do this, hook up LINQPad to your Entity Framework context, press F4 to include a reference to LinqKit.dll, and LinqKit as a namespace import. Let me know if you need additional guidance in how to do this.

StriplingWarrior
OK, I'll have a go at this tomorrow when I'm back at work but it seems like a reasonable way to get the SQL out...
Calanus
Yep that does the job - thanks.
Calanus
A: 

Hi StriplingWarrior,

Could you give me additional guidance in how to hook up LINQPad to my Entity Framework context, and how to import additional namespaces? I tried with simple queries but I received an annoying "; expected" error. I don't know whether it is a problem with the imports or an issue regarding the query. The query is simple as "from product in MyModel.Product select product". This simple query didn't work. Hope you can help me with this.

Mauricio
For future reference, this should probably have been a separate question, or a comment on my posting, rather than an "Answer". I'd suggest you check the Language dropdown at the top of your query tab. If you're on C# Expression, your query should work fine. If you're on C# Statement(s), it expects you to say something like `var p = Product; p.Dump();`. (You don't need `MyModel` if the Database is your entity context, because you're working from within the context). LINQPad has pretty good documentation and samples: I'd suggest you look through it.
StriplingWarrior
A: 
Renzo