Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LINQ - Columns to Rows

Status
Not open for further replies.

rogerzebra

Technical User
May 19, 2004
216
0
0
SE
Hi All,
I clearly need someone to help me with a piece of code that I found online that I have hard to understand and apply it to my project. I'm totally new to C# and LINQ and I would really appreciate if someone could help me out due to the short deadline. So, I'm trying to read in the data from the EDM(Entity framework)to show the column records as rows like transpose a table it's just this has to be read straight from the database. Thanks for your help in advance it is really appreciated.

from
ID Date Amount
1 2/1/2012 100
2 2/2/2012 200
3 2/3/2010 300

to...
ID 1 2 3
Date 2/1/2012 2/2/2012 2/3/2012
Amount 100 200 300


Code:
public static IEnumerable<IEnumerable<T>> Transpose<T>(this IEnumerable<IEnumerable<T>> source)
             {
            
                     return from row in source 
                     from col in row.Select((x, i) => new KeyValuePair<int, T>(i, x)) 
                     group col.Value by col.Key into c select c as IEnumerable<T>; 
            }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top