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, 7:02 AM EDT
Its working
Sanjay ShekhawatPosted Dec 8, 2021, 6:37 AM EST
Thanks ...
Trung Do ThanhPosted Dec 28, 2020, 11:01 PM EST
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 19, 2017, 11:13 PM EST
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, 1:12 AM EDT
Thank you so much. Saved my day
Rahul Kumar SaxenaTOP 100Posted Apr 18, 2015, 10:31 PM EDT
Thanks a lot... Gowtham Rajamanickam
Gowtham RajamanickamTOP 100Posted Apr 18, 2015, 8:03 PM EDT
Good one