按基于日期的对象的嵌套列表进行分组

时间:2014-02-28 20:24:07

标签: c# asp.net linq lambda

大家好我有这样的课程:

   public class supervisorAnswerQuesttionPres
    {
        public string date { set; get; }
        public List<string> questionList { set; get; }
        public List<string> answerList { set; get; }
    }

我有一个返回此类列表的查询

     List<PresentClass.supervisorAnswerQuesttionPres> temp
            = dbconnect.tblAnswerLists
                       .Where(i => i.StudentNum == studentNumber && i.username==objstu.Return_SupervisorUserName_By_StudentNumber(studentNumber))
                       .ToList() // <-- This will bring the data into memory.
                       .Select(i => new PresentClass.supervisorAnswerQuesttionPres
                           {
                               answerList = Return_Answer_List(studentNumber,i.dateOfAnswer.Value.Date),
                               questionList = Return_Question_List(studentNumber, i.dateOfAnswer.Value.Date),
                               date = ConvertToPersianToShow(i.dateOfAnswer.Value.Date)
                           })
                       .OrderByDescending(i => i.date)
                       .ToList();

所以我需要按日期对我的列表 temp 进行分组,然后再次选择所有3列,我的意思是日期,问题列表,答案列表。

我只想在执行此查询后对结果进行分组 我正在使用linq asp.net visual 2012

1 个答案:

答案 0 :(得分:1)

var temp
        = dbconnect.tblAnswerLists
                   .Where(i => i.StudentNum == studentNumber && i.username==objstu.Return_SupervisorUserName_By_StudentNumber(studentNumber))
                   .ToList() // <-- This will bring the data into memory.
                   .Select(i => new PresentClass.supervisorAnswerQuesttionPres
                       {
                           answerList = Return_Answer_List(studentNumber,i.dateOfAnswer.Value.Date),
                           questionList = Return_Question_List(studentNumber, i.dateOfAnswer.Value.Date),
                           date = ConvertToPersianToShow(i.dateOfAnswer.Value.Date)
                       })
                   .GroupBy(i => i.date)
                   .OrderByDescending(i => i.Key)
                   .ToList();

temp实际上属于List<IGrouping<string, PresentClass.supervisorAnswerQuesttionPres>>类型。