c# - join data in linq if only data for object is available from view model - ASP.net -mvc5 -
i have viewmodel extract of multiple model classes. binding data , passing razor partial view show data getting error if 1 of model object null. in business process expected question can use if condition linq--joins i.e. join result if data exist in database or there better way it.
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 _addressdetail in _uow.address_repository.getall() on _student.studentid equals _addressdetail.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 _student.studentid == _studentid select new studentdetailedprofileviewmodel { _studentmodel = _student, _contactdetailmodel = _contactdetail, _addressmodel = _addressdetail , _coursemodel = _studentcourse,_schoolmodel = _school, _campusmodel = _campus}).firstordefault(); _profile._emergencycontactmodel = (from _emergencycontact in _uow.emergencycontact_repository.getall() _emergencycontact.studentid == _studentid select _emergencycontact).tolist(); return _profile; } }// catch { return null; } }
......
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 address _addressmodel { get; set; } public list<emergencycontact> _emergencycontactmodel { get; set; } }
instead of joining, if root entity (student) has navigation properties child collections (and associations configured in entity model) include() them. let linq generate select statement rather trying figure out beforehand.
Comments
Post a Comment