Reputation: 199
I have a viewmodel:
public class CourseViewModel
{
public int CourseID { get; set; }
public string Title { get; set; }
public string page_title { get; set; }
public string titleabbrev { get; set; }
public string Meta { get; set; }
public string intro_1 { get; set; }
public string intro_2 { get; set; }
public string intro_3 { get; set; }
public IEnumerable<course_section> Course_section { get; set; }
public IEnumerable<course_subsection> Course_subsection { get; set; }
public Enrollment Enrollments { get; set; }
}
In my controller I have the following:
var allcourse = db.Course_page
.Where(i => i.titleabbrev == courseabbrev)
.Include(i => i.course_sections.Select(c => c.course_subsections))
.Include(i => i.enrollments);
var viewModel = new CourseViewModel();
viewModel.CourseID = allcourse.Single().CourseID;
viewModel.page_title = allcourse.Single().page_title;
viewModel.titleabbrev = allcourse.Single().titleabbrev;
viewModel.Title = allcourse.Single().Title;
viewModel.intro_1 = allcourse.Single().intro_1;
viewModel.intro_2 = allcourse.Single().intro_2;
viewModel.intro_3 = allcourse.Single().intro_3;
viewModel.Course_section = allcourse
.Single().course_sections
.OrderBy(i => i.Order);
viewModel.Course_subsection = viewModel.Course_section
.SelectMany(c => c.course_subsections)
.Where(i => i.Titleabbrev == sectionabbrev)
.OrderBy(i => i.Order);
return View(viewModel);
I want to send to the view viewmodel.enrollments, however I'm having problems with doing that as I only want to send a single enrollment record to the view in the viewmodel and within the course_page model enrollment is IEnumerable.
Presumably I should take only the single enrollment record when I am creating 'allcourse', however no matter what I try I don't seem to be able to do this and then return it to the view. I've tried for example:
var allcourse = db.Course_page
.Where(i => i.titleabbrev == courseabbrev)
.Where(b => b.enrollments.Single().UserID == User.Identity.GetUserId())
.Include(i => i.course_sections.Select(c => c.course_subsections))
.Include(i => i.enrollments);
What is the best way to send the viewmodel to the view with only the enrollment record that matches the currently logged in userID?
Edit - I've also tried things like:
var allcourse = db.Course_page
.Where(i => i.titleabbrev == courseabbrev)
Where(i => i.Enrollments.Any(b => b.UserID == User.Identity.GetUserId())
.Include(i => i.course_sections.Select(c => c.course_subsections))
.Include(i => i.enrollments);
Testing this in linqpad returned enrollment records where the UserID did not match.
Thanks
Upvotes: 0
Views: 1292
Reputation: 4776
try this query :
var userId = User.Identity.GetUserId();
var viewModel = db.Course_page
.Where(i => i.titleabbrev == courseabbrev)
.Where(i => i.Enrollments.Any(b => b.UserID == userId))
.Select(e => new CourseViewModel{
CourseID = e.CourseID,
page_title = e.page_title,
titleabbrev = e.titleabbrev,
Title = e.Title,
intro_1 = e.intro_1,
intro_2 = e.intro_2,
intro_3 = e.intro_3,
Course_section = e.course_sections.OrderBy(i => i.Order)
.ToList(),
Course_subsection = e.course_sections
.SelectMany(c => c.course_subsections)
.Where(i => i.Titleabbrev == sectionabbrev)
.OrderBy(i => i.Order).ToList()
}).Single();
Upvotes: 1
Reputation: 95
I would suggest you change your starting point to enrolments and then take values from course page and other linked entities..
e.g.
var allcourse = db.Enrollments
.Where(e => e.UserID == User.Identity.GetUserId())
.Select(x => new CourseViewModel {
CourseID = e.Course_page.CourseID,
...
...
Course_section = e.Course_page.course_sections.OrderBy(i => i.Order).FirstOrDefault
...
...
...
});
Upvotes: 1