I am trying to build some custom extensions onto EF (4.3) using ObjectQuery’s ToTraceString() to generate raw SQL from LINQ code.
I’ve noticed that while in some cases the names of the columns in SQL match the property names of the query’s element type, in other cases they are named things like C1, C2, etc. This makes it very difficult for me to do anything with the generated SQL. Does anyone know how to map these column names back to property names (I only care about being able to do this for flat tuples such as anonymous types if that helps, I don’t need anything hierarchical).
A solution that relies on private/internal reflection is perfectly acceptable. I understand that I may have to adjust it with future EF releases. Also, I am only using MS SqlServer (2008+) if it helps.
I just asked and answered a similar question (you can ignore all the DataTable stuff, but it might be useful in that it shows how the mapping is used):
Entity Framework – IQueryable to DataTable
The original post that shows how to get at the mapping between the object property positions and SQL statement column positions can be found here:
How does Entity Framework manage mapping query result to anonymous type?
Note: The second link only provides a mapping of the positions (as integer values) between the object properties and the SQL statement columns…you’ll have to use reflection to get at the actual object property names and then do some kind of string analysis on the SQL statement to get the SQL statement column names (you could use regular expressions for this, but that might be overkill).