GroupBy with Count
我有列表testList
返回,它有5个项目与适当的值。
var testList = _context.LectureReportStudent .Include(x => x.LectureReport) .Include(x => x.LectureReport.Lecture) .Include(x => x.Student) .Where(x => stuIds.Contains(x.LectureReport.LectureId)) .Select(x => new StudentModel { Name = x.Student.FirstName + "" + x.Student.LastName, Position = x.Student.JobTitle, JobId = x.LectureReport.LectureId, StudentId = x.StudentlId, LectureId = x.LectureReportId, Date = x.Date };
我在StudentId
上进一步GroupBy
这个列表。
var result = testList.GroupBy(x => x.StudentId)
每个项目都有以下型号
Date, DaysOnLecture, LectureCount, LectureId, LectureReportId, Name, StudentId, Position
我正在尝试使用CountOnLecture,LectureCount属性。
LectureCount意味着没有。 学生参加讲座的时间(在我的模型中定义)。
DatsOn讲课学生参加这个讲座的日期(在我的模型中定义)。
我对逻辑感到震惊。 任何的想法?
var counts = testList.GroupBy(x => x.StudentId) .Select(g => new { StudentId = g.Key, TotalDays = g.Sum(gg => gg.DaysOnLecture), TotalLecture = g.Sum(gg => gg.LectureCount) });
如果你想要每个LectureId的总数,那么你必须通过LectureId对每个学生的数据做进一步的分组
注意 :目前,这更像是试图澄清您的问题,而不是实际的答案。
我已经开始尝试澄清你想要实现的目标: https : //dotnetfiddle.net/SB02lQ这不是答案; 这是为了澄清你想要完成的事情。
using System; using System.Linq; using System.Collections.Generic; public static class Program { public static void Main() { var LectureReportStudent = GetLectureReportStudent(); var testList = LectureReportStudent .Select(x => new StudentModel { Name = x.Student.FirstName + " " + x.Student.LastName, Position = x.Student.JobTitle, JobId = x.LectureReport.LectureId, StudentId = x.StudentlId, LectureId = x.LectureReportId, Date = x.Date }).GroupBy(x => new { StudentId = x.StudentId, LectureId = x.LectureId }).Select(x => new StudentModel() { StudentId = x.Key.StudentId, LectureId = x.Key.LectureId, DaysOnLecture = x.Count(), LectureCount = -1 // how do we sum all the DaysOnLecture?? }).ToList(); foreach(var t in testList) { Console.WriteLine("StudentId: " + t.StudentId); Console.WriteLine("LectureId: " + t.LectureId); Console.WriteLine("DaysOnLecture: " + t.DaysOnLecture); Console.WriteLine("LectureCount: " + t.LectureCount); Console.WriteLine(); } } public static void Dump(this IEnumerable query, string title) { Console.WriteLine(title); foreach(var i in query) { Console.WriteLine(); Console.Write(i.Name + ", "); Console.Write(i.Position + ", "); Console.Write(i.JobId + ", "); Console.Write(i.StudentId + ", "); Console.Write(i.LectureId + ", "); Console.Write(i.Date + ""); } Console.WriteLine(); } public static List GetLectureReportStudent() { var list = new List (); var lectureId = 0; var studentId = 0; for(var i = 0; i < 24; ++i) { if(i % 12 == 0) { ++studentId; lectureId = 1; } if(i % 3 == 0) ++lectureId; var lrs = new LectureReportStudent() { LectureReport = new LectureReport() { LectureId = lectureId }, Student = new Student() { FirstName = "foo", LastName = "bar", JobTitle = "Job" }, StudentlId = studentId, LectureReportId = lectureId, Date = DateTime.Now }; list.Add(lrs); // Console.WriteLine(studentId + ":" + lectureId); } return list; } public class LectureReportStudent { public LectureReport LectureReport { get; set; } public Student Student { get; set; } public int StudentlId { get; set; } public int LectureReportId { get; set; } public DateTime Date { get; set; } } public class LectureReport { public int LectureId { get; set; } public Lecture Lecture { get; set; } } public class Lecture {} public class Student { public int stuIds { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string JobTitle { get; set; } } public class StudentModel { public string Name { get; set; } public string Position { get; set; } public int JobId { get; set; } public int StudentId { get; set; } public int LectureId { get; set; } public DateTime Date { get; set; } public int DaysOnLecture { get; set; } public int LectureCount { get; set; } public int LectureReportId { get; set; } } }
虽然这不是一个答案,但它可能会帮助您找到答案。