1

我正在尝试这个查询:

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”。

我有一个具有上述字段的实体类。问题是什么????

4

1 回答 1

2

尝试先将其转换为 List<> 。

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();

希望能帮助到你

于 2011-09-24T09:46:45.127 回答