title: Grouping Table Data As Column Headers on Excel Sheet
body: <p>I have a Questionnaire application that I am building for one of our teams and I have having an issue with making the report like they would like it (if at all possible). What I currently have is: </p>
  
  <pre><code>var completedSurveys = (from s in db.SurveyResponses
                                      join d in db.Demographics on s.SurveyID equals d.SurveyID into grpjoin
                                      from d in grpjoin.DefaultIfEmpty()
                                      select new
                                      {
                                          Survey_ID = s.SurveyID,
                                          Survey_Date = s.Survey.SurveyDate,
                                          Question = s.Questions.QuestionTxt,
                                          Response = s.Responses.ResponseTxt,        
                                          Zip_Code = d.ZipCode,
                                          Department = d.CityDepartments.Department,
                                          Ace_Score = s.Survey.AceScore,
  
                                      }).ToList();
  </code></pre>
  
  <p>which after running through the gridview/excel code produces:</p>
  
  <p><a href="https://i.stack.imgur.com/xdrwI.jpg" rel="nofollow noreferrer">My Current Excel Sheet</a></p>
  
  <p>and what they would like is an excel sheet that has the Questions as column headers along with SurveyID, Date, Zip, Department &amp; Score with the responses to the questions as the row data that way everything shows only once. Something like this excel sheet I got from somewhere else:</p>
  
  <p><a href="https://i.stack.imgur.com/j1l5L.jpg" rel="nofollow noreferrer">Example</a></p>
  
  <p>I've tried multiple different groupings for example:</p>
  
  <pre><code>    var completedSurveys = (from s in db.SurveyResponses
                              join d in db.Demographics on s.SurveyID equals d.SurveyID into grpjoin
                              from d in grpjoin.DefaultIfEmpty()
                              group new {s.Questions.QuestionTxt, s.Survey.SurveyDate } by new { s.SurveyID, s.Responses.ResponseTxt, d.ZipCode, d.CityDepartments.Department, s.Survey.AceScore } into g
                              orderby g.Key.SurveyID ascending
                              select new
                              {
                                  Survey_ID = g.Key.SurveyID,
                                  Survey_Date = g.Select(s =&gt; s.SurveyDate),
                                  Response = g.Key.ResponseTxt,
                                  Zip = g.Key.ZipCode,
                                  Department = g.Key.Department,
                                  Ace_Score = g.Key.AceScore
  
                              }).ToList();
  </code></pre>
  
  <p>but I'm still not quite getting what I want. If I have to tell them that what I've already got is as good as it gets, then that's fine but I thought I would at least reach out for some advice. Any Assistance would be appreciated.</p>
  
  <p>Thanks</p>