2017-03-07 114 views
0

我是新來的Asp.Net所以這可能是顯而易見的,但我無法在任何地方找到答案。返回分組查詢結果(C#Asp.Net)

我想以類似於下面的方式對數據進行分組,但是希望將它作爲列表而不是void返回給控制檯,任何想法?

來自實例:https://msdn.microsoft.com/en-us/library/bb545971.aspx

public void GroupBySingleProperty() 
    { 
     Console.WriteLine("Group by a single property in an object:"); 

     // Variable queryLastNames is an IEnumerable<IGrouping<string, 
     // DataClass.Student>>. 
     var queryLastNames = 
      from student in students 
      group student by student.LastName into newGroup 
      orderby newGroup.Key 
      select newGroup; 

     foreach (var nameGroup in queryLastNames) 
     { 
      Console.WriteLine("Key: {0}", nameGroup.Key); 
      foreach (var student in nameGroup) 
      { 
       Console.WriteLine("\t{0}, {1}", student.LastName, student.FirstName); 
      } 
     } 
    } 

編輯:

要嘗試瞭解我的目標,以實現我現在有

public IList<Application> GetGroupedApplication(string userId) 
     { 
      IQueryable<Application> _application; 
      _application = from application 
          in _context.Application 
          where application.UserId == userId 
          select application; 
      return _application.ToList<Application>(); 
     } 

這被用來創建一個強類型該查詢的列表的應用程序視圖。

應用程序有一個名爲applicationOffer場,我想組的結果通過即

被拒絕的申請,其中:

  • 應用1
  • 應用2

無條件應用:

  • Application3
+0

你到底想回到什麼? – DavidG

+0

@DavidG最終我需要一個視圖(理想的模型學生),它提供的控制檯在上面的相同格式的數據,如果這是有道理的? –

回答

0

剛剛擺脫所有的控制檯代碼並返回列表:

public List<IGrouping<string,DataClass.Student>> GroupBySingleProperty() 
{ 
    var queryLastNames = 
     from student in students 
     group student by student.LastName into newGroup 
     orderby newGroup.Key 
     select newGroup; 

    return queryLastNames.ToList(); 
} 
+0

但是後來我無法在視圖中展示這一點,我認爲我的問題對於我試圖實現的內容太不明瞭,編輯可能會有所幫助:) –