The short question is: How do I handle a pivot table in EF4? Any suggestions other than “pivot client-side” will be considered helpful.
One idea is to directly query the database and put the results into a dataset. That’s not a very gentlemanly solution. My only other idea is to use some reflection magic of some sort to dynamically create properties for a temporary object. I have no idea how to handle that in the View.
Here’s the longer explanation and whatnot:
My application has pieces of equipment. It allows the user to “claim” a piece of equipment, and then set a status for that piece of equipment.
The relevant table structure looks like:
EquipmentId, EquipmentName, etc
ClaimId, StatusChangeId, EquipmentId, UserId
StatusChangeId, StatusId, etc
StatusId, StatusName, etc
My query looks up all of the relevant claims for the user (not removed
or archived or new, only ones with statuses set), gets the latest status for each one, tallies them up, and then associates them with their respective equipment.
This big ass dynamic query pivots the data to create a resultset like:
equipment name Total Status1 Status2 Status 3
Computer 4 0 3 1
Monitor 6 1 5 0
The problem is…how do i handle that in EF4? There is no
clean solution, obviously, because the Statuses are dynamic and being returned as
columns. Which means the object would have to magically create
properties on the fly. The statuses are not likely to change in the next few weeks, so as an
interim solution I’ve force fed it an object with the statuses
hardcoded. But that is hideously ugly and kinda defeats the purpose of my
dynamic columns and, well, the entire Status table.
My only other idea is to take and split my beautiful new query that
takes less than a second to run against 1000+ records into a tiny
little query that gets the statuses and counts for a single piece of
equipment at a time and puts it all together in code.
For users that have a few hundred different types of equipment, that would mean a few hundred trips to the database. And I have 2 other queries based on this one that go out and grab the equipment information for users that are associated with the base user…so if there is a user with a couple hundred types of equipment, and he is associated with two other users who have 70 types of equipment each, and they have users associated…you get the picture.
Edit
It occurs to me that this could potentially be solved by putting the columns into a list of a simple object with StatusName and Count as a part of a EquipmentStatus object. Sadly, i still have no idea how to do the mapping. Even though Microsoft acknowledges that handling pivots is an issue, their official word is “we don’t know when it’ll happen”.
EDIT 2
As soon as I have time I will post my final solution. We are finally going to production this week, so it may be a few days. Sorry anyone who comes here looking for an answer.
EF cannot handle such dynamic queries – EF is strongly typed so you need to have at least a type (class) having properties with the same name as columns in result set. This type can be used for materializing records from result set.
So how to use this approach with dynamic result set? It would require something like querying your database first to get names of columns used in your real query, using
Reflection.Emitto create a dynamic assembly with dynamic type (a new one for every query with different result set) and use it in some dynamic way (probably by reflection).DataSet,DataAdapterandDbCommandare classes for queries with dynamic result sets. How are these classes worse than the solution with creating assemblies at runtime? DataSet will allow you implementing this in few minutes!