Search code examples
c#linqasp.net-mvc-5entity-framework-6asp.net-mvc-viewmodel

How to add multiple records in List along with other model in view-model Linq - ASP.NET-MVC5


I am working on ASP.NET-MVC5 applications. I need to pass data model from multiple classes from controller to view so I decided it use ViewModel and use linq to assign values to the view model accordingly. Now I my model, Student can have multiple emergency contact so I am using List but getting error on this part in LINQ query.

enter image description here

ViewModel

public class StudentDetailedProfileViewModel
{
    public StudentDetailedProfileViewModel() { }

    public Student _studentModel { get; set; }
    public Course _courseModel { get; set; }
    public School _schoolModel { get; set; }
    public Campus _campusModel { get; set; }
    public ContactDetail _contactDetailModel { get; set; }
    public List<EmergencyContact> _emergencyContactModel { get; set; }

}

Function that need to return strongly typed binded data

 public StudentCourseSchoolAndCampusViewModel GetCourseByStudentID(int _studentID)
    {
        try
        {
            using (var _uow = new StudentProfile_UnitOfWork())
            {
                var _record = (from _course in _uow.Course_Repository.GetAll()
                              join _school in _uow.School_Repository.GetAll() on _course.SchoolID equals _school.SchoolID
                              join _campus in _uow.Campus_Repository.GetAll() on _course.CampusID equals _campus.CampusID
                              where _course.StudentID == _studentID
                              select new StudentCourseSchoolAndCampusViewModel  {_courseModel= _course, _schoolModel = _school, _campusModel = _campus }).FirstOrDefault();

                return _record;
            }
        }
        catch { return null; }
    }

Solution

  • I have managed to do as following but I am not sure if it is the best practice!!

        public StudentDetailedProfileViewModel GetStudentDetailedProfileByStudentID(int _studentID)
        {
            try
            {
                 using (var _uow = new StudentProfile_UnitOfWork())
                {
                     StudentDetailedProfileViewModel StudentProfileObject = new StudentDetailedProfileViewModel();
    
                    var _profile = (from _student in _uow.Student_Repository.GetAll()
                                    join _contactDetail in _uow.ContactDetail_Repository.GetAll() on _student.StudentID equals _contactDetail.StudentID
                                    join _studentCourse in _uow.Course_Repository.GetAll() on _student.StudentID equals _studentCourse.StudentID
                                    join _school in _uow.School_Repository.GetAll() on _studentCourse.SchoolID equals _school.SchoolID
                                    join _campus in _uow.Campus_Repository.GetAll() on _studentCourse.CampusID equals _campus.CampusID
                                    where _student.StudentID == _studentID
                                    select new StudentDetailedProfileViewModel { _studentModel = _student, _contactDetailModel = _contactDetail, _courseModel = _studentCourse,_schoolModel = _school, _campusModel = _campus}).FirstOrDefault();
    
                    _profile._emergencyContactModel = (from _emergencyContact in _uow.EmergencyContact_Repository.GetAll()
                                                      where _emergencyContact.StudentID == _studentID
                                                      select _emergencyContact).ToList();
    
    
                    return _profile;                
                }
            }//
            catch { return null; }
    
        }