Error: The Entity or Complex Type cannot be constructed in a LINQ to Entities Query

While Fetching data from tables if you face below error…



To avoid this error replace your code with below code:

  1. public ActionResult Index()   
  2. {  
  3.     var students = (from p in objContext.Students  
  4.     join f in objContext.Courses  
  5.     on p.CourseId equals f.CourseId  
  6.     select new   
  7.     {  
  8.         Id = p.Id,  
  9.         Name = p.Name,  
  10.         DateOfBirth = p.DateOfBirth,  
  11.         EmailId = p.EmailId,  
  12.         Address = p.Address,  
  13.         City = p.City,  
  14.         CourseName = f.CourseName  
  15.     }).ToList()  
  16.         .Select(x = > new Student()   
  17.         {  
  18.         Id = x.Id,  
  19.         Name = x.Name,  
  20.         DateOfBirth = x.DateOfBirth,  
  21.         EmailId = x.EmailId,  
  22.         Address = x.Address,  
  23.         City = x.City,  
  24.         CourseName = x.CourseName  
  25.     });  
  26.     return View(students.ToList());  
  27. }  
Ebook Download
View all
Learn
View all