Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I do a cast to solve an explicit conversion needed error?
    primarykey
    data
    text
    <p>I’m trying to access my list in the view like this: </p> <pre><code> List&lt;xxxx.Models.Enrollment&gt; newStudentEnrollment=ViewBag.studentrecord; </code></pre> <p>But I get an error message that says: Cannot implicitly convert type 'object' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?)… I’ve tried using FirstOrDefault() and ToList() like I’ve seen worked for others in this forum but neither worked for me. Also tried removing the ToList() from here 'ViewBag.studentrecord = newRecord.ToList();' in the controller </p> <p>Here’s what I’m doing in my controller….</p> <pre><code> var StudentRecord = (from E in db.Enrollments join S in db.Students.Where(r =&gt; r.StudentID == intStudentNumber) on E.StudentID equals S.StudentID into JoinedStudent from AllStudent in JoinedStudent.DefaultIfEmpty() select new { CourseID=E.CourseID, StudentID=E.StudentID, Date=E.Date, InstructorFullName=E.InstructorFullName, classDays=E.classDays, listOfDays=E.listOfDays, listOfTeachers=E.listOfTeachers, IsStudentEnroll = AllStudent != null ? true : false }).ToList(); var newRecord = from r in StudentRecord where r.StudentID==67 select r; ViewBag.studentrecord = newRecord.ToList(); </code></pre> <p>Seeing that my data is of type List I thought I could do this…</p> <pre><code> List&lt;xxxx.Models.Enrollment&gt; newStudentEnrollment=ViewBag.studentrecord; </code></pre> <p>How do I do a cast with this or make it work in any way?</p> <p>Thanks very much for any help. </p> <p>EDIT:</p> <p>So I tried this: </p> <pre><code> List&lt;xxxx.Models.Enrollment&gt; newStudentEnrollment = (List&lt;xxxx.Models.Enrollment&gt;)ViewBag.studentrecord; </code></pre> <p>Unable to cast object of type 'System.Collections.Generic.List<code>1[&lt;&gt;f__AnonymousType9</code>7[System.Int32,System.Int32,System.String,System.String,System.String,System.String,System.Boolean]]' to type 'System.Collections.Generic.List`1[xxxx.Models.Enrollment]'.</p> <p>Also tried:</p> <pre><code> List&lt;xxxx.Models.Enrollment&gt; newStudentEnrollment = ViewBag.studentrecord as List&lt;xxxx.Models.Enrollment&gt;; </code></pre> <p>but newStudentEnrollment was null. I think it has to do with the join clause. It combined two objects so I somehow have to create a generic object of type list. Only I don't know how...any ideas? Thanks.</p> <p>UPDATE</p> <p>I changed the type from a Join to Enrollment like this. var newEnrollment = from e in context.Enrollment where e.SomeId ==someNumber select e;</p> <p>Than in the view this worked: List newStudentEnrollment = (List)ViewBag.studentrecord;</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload