Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8298861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:56:17+00:00 2026-06-08T15:56:17+00:00

I am working in MVC3, C# and using Razor. I don’t work with MVC

  • 0

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;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T15:56:20+00:00Added an answer on June 8, 2026 at 3:56 pm

    Well you can always do this for the query portion, I’m guessing that’s where you are getting tripped up:

    var schools = db.Schools
         .Where(x => x.State == "<Whatever>")
         .Select(x => new {
              School = x,
              Bobs = x.Where(y => y.MiddleName == "Bob")
         }).ToList();
    
    List<Schools> schoolsView = new List<Schools>();
    foreach(var x in schools)
    {
         schoolsView.Add(new SchoolsViewModel(){
              //Set Properties here
              SchooldID = x.School.ID,
              SchoolName = x.School.Name,
              Students = x.Bobs.ToList() //You can project here if needed.
         };
    }
    
    return schoolsView;
    

    Then for your view you can do this any number of ways but if you have the concrete ViewModel with the fixed output it’s just nested loops:

    <ul>
    @foreach(var m in Model)  //Assuming your model is a list of SchoolViewModels
    {
         <li>@m.SchoolName
              <ul>
                   @foreach(var s in m.Students)
                   {
                        <li>@s.Name</li>
                   }
              </ul>
         </li>
    }
    </ul>
    

    You also have cute options for doing things like String.Join(",", Model.Students) to output the list but this is up to you.

    Edit: Additional Clarification

    You will also want to change your models a tad, I’m not sure what EF version you are using but in EF4.1+ you can specify your navigation properties to remove the need for an explicit join condition (which is the right approach in the end).

    http://msdn.microsoft.com/en-us/library/bb738520.aspx

    So your Schools model would become:

    public class School
    { 
       public int SchoolId {get; set;} 
       public string Schoolname {get; set;} 
       public int StateId {get; set;} 
       public virtual IList<Student> Students {get; set; }
    } 
    

    And then in your DB configuration (If you are using fluent the configuration would be:

    modelBuilder.Entity<School>()
         .HasMany(x => x.Students).WithRequired();
    

    Then you gain the power of doing

    db.Schools
        .Where(x => x.Name.Contains("Some Value"))
        .Include(x => x.Schools.Where(x => x.MiddleName.Contains("SomeValue")))
        .ToList();
    

    Which even makes my prior query even more simple without needing to use ambiguous type definitions.

    Hopefully this clarification helps a bit further.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on my first MVC3 application at work (using the Razor view
I am working on an MVC3 application using the Razor syntax and I have
I'm working on an MVC3 (with Razor) / jQuery Mobile application and am having
I am working on project using MVC 3.0(Razor framework). I am trying to get
I am working on a .NET MVC 3, razor site and I want to
FYI, I'm working on my first MVC web application using MVC 3 and Razor
I am working in ASP.NET MVC 3 application, I am using razor view. I
I'm working on an MVC3 web application using the razor view engine. I'm looking
I'm working on a page using ASP.NET MVC3, Razor and Entity Framework. I used
I am working in ASP.NET MVC3 using Razor. I have a situation where I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.