[Note After Answer: I am actually querying in memory-objects and that's why ToTraceString
doesn't work. I added this to save the reader potential time from reading my long post].
I'm using a ToTraceString
command when trying to inspect how my LINQ queries end up looking. However, today my query got a bit complicated, involving a join
and all of the sudden, I get this error when I try to Trace my String:
Unable to cast object of type 'd__7a`1[EGSLanguageProviderShared.DTODataType]' to type 'System.Data.Objects.ObjectQuery'.
My Query, and subsequent invocation of ToTraceString
is as follows (note that System.Data.Entity has to be referenced in order for this to work). Both objects I'm querying (langDTs and langInstructionsAVDTs) are Entity Framework (.Net 3.5) objects from the same database. My Where Clause (== av.InstructionAVKey) uses a simple Value Collection Class, nothing to see there.
IEnumerable<DTODataType> dts =
(from langDT in langDTs
join langIAVDT in langInstructionsAVDTs
on langDT.DataTypeKey equals langIAVDT.DataTypeKey
where langIAVDT.InstructionAVKey == av.InstructionAVKey
select langDT).Distinct();
var sql = ((System.Data.Objects.ObjectQuery)dts).ToTraceString();
Any ideas on how I could see the LINQ translation of this Join? ::- ). I noticed that System.Data.Objects has more types of queries, but I can't get any of the ones which seem more relevant to this case, to work.
LATER EDIT:
As you recommended, I tried changing the IEnumerable to an IQueryable but that resulted in a type incompatibility compilation error ::- /.
After doing an explicit cast, I got the same error, but at Runtime (Unable to cast object of type '<DistinctIterator>d__7a1[EGSLanguageProviderShared.DTODataType]' to type 'System.Linq.IQueryable
1[EGSLanguageProviderShared.DTODataType]'.`)
Additional code: my objects langDTs and langInstructionsAVDTs are:
List<DTOInstructionActiveValueDataType> langInstructionsAVDTs = CurrentLPInstructionManager.GetInstructionsActiveValuesDataTypes((from avKey in langInstructionsAVs select avKey.InstructionAVKey).Distinct().ToArray());
List<DTODataType> langDTs = _LPDataTypeManager.GetDataTypes((from dt in langInstructionsAVDTs orderby dt.DataTypeKey select dt.DataTypeKey).Distinct().ToArray());
So these objects are indeed queried immediately because they are Lists ::- ). As for DTODataType and DTOInstructionActiveValueDataType, they are simple Value Collection Classes, just public Properties, that's all.
EVEN LATER EDIT
Might be of interest that at their root, the objects I'm using are indeed declared as ObjectQuery back in the deepest layer (Entity Framework):
public global::System.Data.Objects.ObjectQuery<instructions> instructions
However, as I bring the data from over the Data Access Layer, I am converting them to Data Transfer Objects (the DTO-prefixed classes you keep seeing), which are simple Value Collection Classes (a property-by-property map of the Entity Objects which I use in order to keep the Data Model completely separated from the View and also to execute any data post-processing in the Presentation side).
There are two methods available in Linq to perform Join Operations. They are as follows: Join: This operator is used to join two data sources or collection based on common property and return the data as a single result set.
In LINQ, an inner join is used to serve a result which contains only those elements from the first data source that appears only one time in the second data source. And if an element of the first data source does not have matching elements, then it will not appear in the result data set. Here Join and Inner join are the same.
Joining Operator: Join. The joining operators joins the two sequences (collections) and produce a result. The Join operator operates on two collections, inner collection & outer collection. It returns a new collection that contains elements from both the collections which satisfies specified expression. It is the same as inner join of SQL.
The join clause always takes two data source, the elements present the data source must contain some property so that it can compare with other data source. The result of the join clause depends upon which type of join clause is used. The most common types of the join are:
Instead of typing your variable as IEnumerable<DTODataType>
try IQueryable<DTODataType>
, or even var.
I'm guessing somewhere in there your query is getting executed, and the results being stored as IEnumerable
, and therefore no longer able to be cast as ObjectQuery
EDIT
Can you please expand your code snippet to show were both langDTs and langInstructionsAVDTs come from?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With