返回分组查询结果(C#Asp.Net)

时间:2017-03-07 00:15:06

标签: c# asp.net

我是Asp.Net的新手,所以这可能很明显,但我无法在任何地方找到答案。

我正在尝试以类似于下面的方式对数据进行分组,但是想要将其作为列表返回而不是向控制台返回空白,任何想法?

示例来自: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>();
        }

这用于从此查询中为列表创建强类型的Application视图。

应用程序有一个名为applicationOffer的字段,我想按结果分组结果

拒绝申请:

  • 应用1
  • 应用2

无条件申请:

  • Application3

1 个答案:

答案 0 :(得分: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();
}