While Fetching data from tables if you face below error…

To avoid this error replace your code with below code:
- public ActionResult Index()
- {
- var students = (from p in objContext.Students
- join f in objContext.Courses
- on p.CourseId equals f.CourseId
- select new
- {
- Id = p.Id,
- Name = p.Name,
- DateOfBirth = p.DateOfBirth,
- EmailId = p.EmailId,
- Address = p.Address,
- City = p.City,
- CourseName = f.CourseName
- }).ToList()
- .Select(x = > new Student()
- {
- Id = x.Id,
- Name = x.Name,
- DateOfBirth = x.DateOfBirth,
- EmailId = x.EmailId,
- Address = x.Address,
- City = x.City,
- CourseName = x.CourseName
- });
- return View(students.ToList());
- }


bhaPosted Apr 12, 2024, 11:02 AM
Its working
Sanjay ShekhawatPosted Dec 8, 2021, 11:37 AM
Thanks ...
Trung Do ThanhPosted Dec 29, 2020, 4:01 AM
Thanks, I don't know why but it works. We can select to anonymous object, or can create other Model to select direct on the query.
Burak TarhanlıPosted Dec 20, 2017, 4:13 AM
Thank you. But i've question. How can i use it with async and await? I need to use .ToListAsync()
Debasmit SamalPosted Mar 14, 2016, 5:12 AM
Thank you so much. Saved my day
Rahul Kumar SaxenaPosted Apr 19, 2015, 2:31 AM
Thanks a lot... Gowtham Rajamanickam
Gowtham RajamanickamPosted Apr 19, 2015, 12:03 AM
Good one