我希望在实体框架中使用LINQ查询获取学生详细信息及其上课时间列表。
我使用以下查询来绑定学生详细信息列表以及他们参加的总小时数。
var students = (from pa in db.tblstudents
select new studentModel
{
Name = pa.Name,
StudentID = pa.ID,
RollNO = pa.rollNO,
Department = pa.Department,
Phone = pa.Phone,
Address = pa.Address,
TotalHours = (from time in tblhours
where time.studentID = pa.ID
select time.hours).Sum()
}).Distinct().AsQueryable().ToList();
my studentModel looks like below:-
public class StudentModel
{ public string Name { get; set; }
public int StudentID { get; set; }
public string RollNO{ get; set; }
public string DepartMent{ get; set; }
public long Phone{ get; set; }
public string Address{ get; set; }
public string Address2{ get; set; }
public decimal TotalHours{ get; set; }
}
tblhours和tblstudent与studentid有关,如果我想在特定时间段内让所有学生都参加,我该怎么办呢?
当我尝试上述查询时,我得到错误为“LINQ to Entities中仅支持无参数构造函数和初始值设定项”
当我使用子查询选择总时数时,我认为我错了...
答案 0 :(得分:0)
确保StudentModel
类
TotalHours = pa.tblhours.Sum(time=>time.hours)