我已将列表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
};
我在GroupBy
上进一步StudentId
此列表。
var result = testList.GroupBy(x => x.StudentId)
每个项目都有以下型号
Date, DaysOnLecture, LectureCount, LectureId, LectureReportId, Name, StudentId, Position
我正在尝试使用Count of DaysOnLecture,LectureCount属性。
LectureCount意味着没有。学生参加讲座的时间(在我的模型中定义)。
DatsOnLecture没有学生参加这个讲座(在我的模型中定义)。
我对逻辑感到震惊。有什么想法吗?
答案 0 :(得分:0)
注意:就目前而言,这更像是为了澄清您的问题而非实际答案。
我创办了一个小提琴试图澄清你想要完成的事情: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<StudentModel> 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<LectureReportStudent> GetLectureReportStudent()
{
var list = new List<LectureReportStudent>();
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; }
}
}
虽然这不是一个答案,但它可能会帮助您找到答案。
答案 1 :(得分:0)
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对每个学生的数据进行进一步分组