0

Possible Duplicate:
Is it possible to Pivot data using LINQ?

I'm just starting to get into using LINQ inside of my C# projects instead of using stored procedures in my database. Im a LINQ rookie.

What would this SQL query look like in LINQ format?

SELECT Month,
    [1] VideoCount,
    [2] GameCount,
    [3] FlashCount,
    [4] PictureCount,
    [5] JokeCount
FROM 
(
    SELECT MediaTypeID, DATENAME(m, MediaDate) Month
    FROM Media
    WHERE DATEPART(YEAR, MediaDate) = 2012
) src
PIVOT
(
    COUNT(MediaTypeID)
    FOR MediaTypeID IN ([1], [2], [3], [4], [5])
) piv
ORDER BY MONTH(Month +' 1900')

My Table looks like:

And my SQL results look like:

enter image description here

Community
  • 1
  • 1
Maddhacker24
  • 1,841
  • 7
  • 26
  • 32
  • Need more information such as which object are you wanting to perform the query on? Are you wanting to use an extension method? – Botonomous Jan 23 '13 at 16:21
  • 3
    Check this stack post: http://stackoverflow.com/questions/167304/is-it-possible-to-pivot-data-using-linq – Kevin Dahl Jan 23 '13 at 16:25
  • You did ask the SQL question last night. I'd strongly suggest you to do this that way, even if you are not familiar with T-SQL. – Rikki Jan 23 '13 at 17:08
  • Mohammad, I did. Im calling the query inside of my project using var Results = DB.Database.SqlQuery(SQL); where SQL is the query text. Since the query is inside my project I thought it might be better to be written as LINQ? I'm not sure though. – Maddhacker24 Jan 23 '13 at 17:22

0 Answers0