开发者

Error in linq join query in MVC 4

开发者 https://www.devze.com 2023-04-07 11:17 出处:网络
I am trying this query: public ActionResult Index() { var topics = from t in db.Topics join subs in db.Subjects on t.SubID equals subs.SubID

I am trying this query:

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
       开发者_StackOverflow   {
               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);
}

But it causes the following Error:

The entity or complex type 'MySiteModel.ViewModel' cannot be constructed in a LINQ to Entities query.

I have an Entitity Class with above fields. What is the problem? ????


Try convert it to List<> first.

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

Hope it helps

0

精彩评论

暂无评论...
验证码 换一张
取 消