I am working in MVC3, C# and using Razor. I don't work with MVC on a consistent basis so I am constantly having to relearn things... I want a view that displays a list of schools (from a specified state) with a nested list of the students (from that school) that have the middle name 'Bob'.
Seems simple enough but so far I am not able to find a solution.
Ex: VIEW
Results for Maryland
My First School: - Billy Bob Johnson - Sally Bob Simpson - Adam Bob Jones
Another School: - Arnold Bob Smith - Cathy Bob Powers - Jenny Bob Smith
The Other School: - Barney Bob Edson - Sue Bob Purdee - Alan Bob Warfield
public class School
{
public int SchoolId {get; set;}
public string Schoolname {get; set;}
public int StateId {get; set;}
}
public class Student
{
public int StudentId {get; set;}
public int SchoolId {get; set;}
public string Firstname {get; set;}
public string Middlename {get; set;}
public string Lastname {get; set;}
}
public class SchoolViewModel
{
public int SchoolId {get; set;}
public string Schoolname {get; set;}
public IEnumerable<Student> Bobs { get; set; }
}
Getting a list by doing a SELECT with JOINS will give me a data set that needs additional work in order to display in the VIEW as illustrated above. I am thinking that there is a way to grab the nested list of students in a way that allows the data set to act like a mulitidimensional array. But I am not finding examples of that approach - leading me to think that THAT is not the way... ?? How do I grab the data in a way that makes the best use LINQ / MVC / ViewModel etc... ? :)
I was trying something like this - but don't know if this is the right direction or not - and it gave me an error when I tried to set the Bobs. :)
I get an error like: LINQ to Entities does not recognize the method 'System.Linq.IQueryable`1[stuff] GetBobs(stuff)' method, and this method cannot be translated into a store expression.
private IQueryable<SchoolViewModel> GetSchools(int StateId)
{
var query = from s in db.Schools
where s.State == StateId
orderby s.Schoolname
select new SchoolViewModel
{
SchoolId = s.SchoolId,
Name = s.Name,
Bobs = GetBobs(s.SchoolId)
};
var results = query;
return results;
}
private IQueryable<Student> GetBobs(int id)
{
var query = from s in db.Students
where s.Middlename == 'Bob'
where s.SchoolId == id
select s;
var results = query;
return results;
}