Adalat  Khan

Adalat Khan

  • 622
  • 1.5k
  • 845.3k

Retrieve specific fields from a database table using LINQ

Aug 14 2018 5:15 PM
I have an Action Method in MVC application that retrieves the list of Students from a database table StudentsInfo using LINQ query. When i retrieve all the columns from the table then the Query works but when i try to retrieve some specific columns from the table then i get the following error message:
 
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[<>f__AnonymousType6`1[System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MVCApplication.StudentsInfo]'.
 
What I have tried:
 
Following is the action Method
 
public ActionResult GetAllStudents()
{
//Retrieve data from StudentsInfo table
var result = from d in db.StudentsInfo
select new
{
d.StdRegNumber,
d.StdName,
d.StdDoBirth
};
return View(result.ToList());
}
 
Thanks in advance

Answers (5)