2017-03-07 115 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

但是后来我无法在视图中展示这一点,我认为我的问题对于我试图实现的内容太不明了,编辑可能会有所帮助:) –