Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8205903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:27:16+00:00 2026-06-07T08:27:16+00:00

The short question is: How do I handle a pivot table in EF4? Any

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T08:27:18+00:00Added an answer on June 7, 2026 at 8:27 am

    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.Emit to 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).

    One idea is to directly query the database and put the results into a
    dataset. That’s not a very gentlemanly solution.

    DataSet, DataAdapter and DbCommand are 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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Short question: Do any of MS's built in Data Objects support INotifyPropertyChanged? Long explination:
Here is a short question about how best to handle text truncation. Do you
Short question, is the following ok: struct X { A& x; A y; X()
Short question: Is it possible to detect window.open() in a UIWebView using the UIWebViewDelegate
Short Question: Can I specify wildcards for custom console folding? If so, what is
Short Question: What is the importance of isomorphic functions in programming (namely in functional
Short question - how do you define your view models? Here are some of
Short question: Is it possible to do a redirection, say when a user isn't
Short question: Is the parent of an entity group included in that entity group
Short question. How do you go about transposing UML diagrams into code? The class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.