Honar
Honar

Reputation: 17

Error in linq join query in MVC 4

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
          {
               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? ????

Upvotes: 1

Views: 688

Answers (1)

ysrb
ysrb

Reputation: 6740

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

Upvotes: 2

Related Questions