LINQ 联查多表数据并封装到ViewModel的实现
public List<MyTask> GetPagedTaskList(int pageIndex, int pageSize, string stuNum, int taskTypeId) {
DbSet<T_TaskParticipation> taskParticipations = db.Set<T_TaskParticipation>(); DbSet<T_TaskInformation> taskInformations = db.Set<T_TaskInformation>(); DbSet<T_TaskType> taskTypes = db.Set<T_TaskType>(); DbSet<T_MemberInformation> memberInformations = db.Set<T_MemberInformation>();
List<MyTask> myTaskList = (from taskParticipation in taskParticipations join taskInformation in taskInformations on taskParticipation.TaskId equals taskInformation.TaskId join taskType in taskTypes on taskInformation.TaskTypeId equals taskType.TaskTypeId join memberInformation in memberInformations on taskInformation.TaskSender equals memberInformation.StuNum where taskParticipation.TaskReceiver == stuNum && taskInformation.TaskTypeId == taskTypeId select new MyTask() { TaskId=taskParticipation.TaskId, TaskSender=taskInformation.TaskSender, TaskName=taskInformation.TaskName, TaskTypeId=taskInformation.TaskTypeId, TaskContent=taskInformation.TaskContent, TaskBegTime=taskInformation.TaskBegTime, TaskEndTime=taskInformation.TaskEndTime, TaskReceiver=taskParticipation.TaskReceiver, TaskGrade=taskParticipation.TaskGrade, IsRead=taskParticipation.IsRead, IsComplete=taskParticipation.IsComplete, TaskSenderName=memberInformation.StuName, TaskTypeName=taskType.TaskTypeName }).OrderBy(taskParticipation => taskParticipation.IsRead).ThenBy(taskParticipation => taskParticipation.TaskId).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
return myTaskList;
}