2011-09-24 38 views
1

我想這個查詢:錯誤中的LINQ MVC連接查詢4

public ActionResult Index() 
{ 
var topics = from t in db.Topics 
      join subs in db.Subjects on t.SubID equals subs.SubID 
      join mems in db.Members on t.MemberID equals mems.MemberID 
      select new ViewModel 
      { 
       TopicID = t.TopicID, 
       TDate = t.TDate, 
       Title = t.Title, 
       FileName = t.FileName, 
       Displays = t.Displays, 
       Description = t.Description, 
       SubName = subs.SubName, 
       FLName = mems.FLName 
      }; 
    return View(topics); 
} 

但它會導致以下錯誤:

實體或複雜類型「MySiteModel.ViewModel」不能在構造LINQ to Entities查詢。

我有一個Entitity類與上述字段。 問題是什麼? ????

回答

2

嘗試將它轉換爲列表<>首先。

var topics = (from t in db.Topics 
      join subs in db.Subjects on t.SubID equals subs.SubID 
      join mems in db.Members on t.MemberID equals mems.MemberID 
      select new ViewModel 
      { 
       TopicID = t.TopicID, 
       TDate = t.TDate, 
       Title = t.Title, 
       FileName = t.FileName, 
       Displays = t.Displays, 
       Description = t.Description, 
       SubName = subs.SubName, 
       FLName = mems.FLName 
      }).ToList(); 

希望它可以幫助