2013-11-10 11 views
0

我試圖訪問這樣的觀點我的列表:如何才能解決顯式轉換所需的錯誤?

List<xxxx.Models.Enrollment> newStudentEnrollment=ViewBag.studentrecord; 

但我得到一個錯誤,指出消息:無法鍵入「對象」隱式轉換爲「System.Collections.Generic.List」。存在明確的轉換(你是否缺少演員?)... 我試過使用FirstOrDefault()和ToList(),就像我見過的在這個論壇上爲別人工作,但都沒有爲我工作。還嘗試從這裏刪除ToList()'ViewBag.studentrecord = newRecord.ToList();'在控制器中

這是我在我的控制器中所做的...。

var StudentRecord = (from E in db.Enrollments 
          join S in db.Students.Where(r => 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(); 

看到我的數據,我想我能做到這一點類型列表...

List<xxxx.Models.Enrollment> newStudentEnrollment=ViewBag.studentrecord; 

如何做一個鑄有這樣或使其以任何方式工作?

非常感謝您的幫助。

編輯:

所以,我想這樣的:

List<xxxx.Models.Enrollment> newStudentEnrollment = (List<xxxx.Models.Enrollment>)ViewBag.studentrecord; 

無法轉換類型的對象「System.Collections.Generic.List 1[<>f__AnonymousType9 7 [System.Int32,System.Int32,System.String ,System.String,System.String,System.String,System.Boolean]]'鍵入'System.Collections.Generic.List`1 [xxxx.Models.Enrollment]'。

也試過:

List<xxxx.Models.Enrollment> newStudentEnrollment = ViewBag.studentrecord as List<xxxx.Models.Enrollment>; 

但newStudentEnrollment爲空。我認爲這與加入條款有關。它結合了兩個對象,所以我不得不創建一個類型列表的通用對象。只有我不知道......有什麼想法?謝謝。

UPDATE

我從加入到招生這樣的改變類型。 var newEnrollment = from e in context.Enrollment where e.SomeId == someNumber select e;

比在視圖這工作: 列表newStudentEnrollment =(List)ViewBag.studentrecord;

回答

0

你需要投你ViewBag.studentrecordList<xxxx.Models.Enrollment>

List<xxxx.Models.Enrollment> newStudentEnrollment = (List<xxxx.Models.Enrollment>)ViewBag.studentrecord; 
0

使用本:

List<xxxx.Models.Enrollment> newStudentEnrollment=ViewBag.studentrecord as List<xxxx.Models.Enrollment>; 
相關問題