Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Is there a way to write a Pivot() using a LINQ query against a DataTable?

Posted on 2010-11-26
5
Medium Priority
?
746 Views
Last Modified: 2013-11-11
I've got a bit of data problem I'm trying to figure out.


Essentially, I'm dealing with an Entity/Attribute/Value type of schema,
similar to IDS. Currently, we're sitting on an Oracle backend, but will
eventually move to something new (NOSQL, postgres,...) So, rather than push
to work to the database using a command (pivot) that might or might not be
supported, we want to do the work in code.

So, this is where we are...
When I initially query the database, I can a dataset back in the form:
NOTE:: I will


ENTITY TYPES
-----------------
ENT_TYPE_ID  ENTID  ENTNAME
======================
1                      1         a1
1                      2         a2
1                      3         a3
1                      4         a4
2                      1         b1
2                      2         b2
2                      3         b3
2                      4         b4

NOTE:: Each ENT_TYPE_ID can have 1-N ENTIDs

ID  ENTID VAL
=============
1   1     w1
1   2     x1
1   3     y1
1   4     z1       I'd like this to map to a table looking like this:
2   1     w2
2   2     x2           (Cols) a1   a2   a3   a4
2   3     y2           ---------------------------          
2   4     z2                     w1   x1   y1   z1
3   1     w3                    w2   x2   y2   z2
3   2     x3                     w3   x3   y3   z3
3   3     y3                     w4   x4   y4   z4
3   4     z3
4   1     w4
4   2     x4
4   3     y4
4   4     z4

The datatable will become the datasource for an Infragistics UltraDataGrid.
Our columns & data set are pretty dynamic in nature.
We'd like to do this as officially as possible in C#.

In Flex/Actionscript (and I assume Java), there's the notion of creating a dynamic
object based on named/value pairs. This works well in decoding JSON objects.
I'm kind of trying to approximate this in C#.

The 1st thought is to go to an array of dictionaries: ISortedDictionary<key, val>.
Now the question becomes, how to get from that to a DataTable?
Even though I'm using an list of items, I don't think it'll be directly bindable.
Dictionary objects can't be directly used to bind as a data source to a grid (or list, etc).

What's the best way to massage the array/list of dictionaries into datatable?
I know I could do it the brain dead way of iterating..., but is there a slicker
better performing approach I can take?

I took a look @ Tuples in C# 4.0.  As near as I can tell , they won't work in my implementation.
I am/will be dealing with a dynamic number of entities (1->N). The system should respond & behave
accordingly when presented with any number of Entities (Ent IDs).

Is there a way to do this in LINQ? I've been able to google a bunch of examples, but they
all dealt with a fixed number of entities.  I only know the number of entities @ runtime, so I ve
need to create a LINQ query that can PIVOT() on a dynamic set of entities.

Can LINQ somehow provide a PIVOT solution like that?
 
Performance considerations could be an issue here. It's unclear to me what the volume of through put will be.

Any help would be greatly appreciated here...

Thanks,
JohnB



0
Comment
Question by:jxbma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 41

Expert Comment

by:ralmada
ID: 34231712
0
 
LVL 1

Author Comment

by:jxbma
ID: 34232999
Unfortunately, these links do not help.
I know how to do a "Pivot()" when I have a fixed number of columns
and I know them @ compile time.

The real issue I'm trying to solve is how to write a LINQ query for a dynamic set of columns.
(I only know them @ run time...)
0
 
LVL 41

Accepted Solution

by:
ralmada earned 2000 total points
ID: 34241021
0
 
LVL 21

Expert Comment

by:tovvenki
ID: 34259502
0
 
LVL 1

Author Closing Comment

by:jxbma
ID: 34264688
Winner, winner, chicken dinner!! This is awesome; exactly what I've been looking for. Creating an anonymous type is the way to go. (Dynamic class & JSON object)
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question