MVC 4中的linq连接查询出错

时间:2011-09-24 09:42:44

标签: asp.net-mvc linq asp.net-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);
}

但它会导致以下错误:

无法在LINQ to Entities查询中构造实体或复杂类型“MySiteModel.ViewModel”。

我有一个包含上述字段的Entitity Class。 问题是什么? ????

1 个答案:

答案 0 :(得分: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();

希望有所帮助