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 7902353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:28:08+00:00 2026-06-03T09:28:08+00:00

I am trying to select a multiple table from the database using LINQ in

  • 0

I am trying to select a multiple table from the database using LINQ in MVC3 Razor. What do I need to change to my codes in order for me to view multiple data from multiple table in the view?

This is my Model

public class DailyAttendance
{
    [Key]
    public string pnp { get; set; }
    public string student_id { get; set; }
    public string mth_yr_id { get; set; }
    public string D01 { get; set; }
    public string D02 { get; set; }
} // and so on

public class TitasDb : DbContext
{
    public DbSet<DailyAttendance> DailyAttendance { get; set; }
    public DbSet<MonthYear> MonthYear { get; set; }
    // and so on

}

This is how I use LINQ in the controller

public class AttendanceController : Controller
{
    TitasDb _attendanceModel = new TitasDb();

    public ActionResult Index()
    {
        var model = from s in _attendanceModel.Student
                    join ssc in _attendanceModel.StudentSchoolClass on s.student_id equals ssc.student_id
                    join sc in _attendanceModel.SchoolClass on ssc.school_class_id equals sc.school_class_id
                    join da in _attendanceModel.DailyAttendance on s.student_id equals da.student_id
                    join my in _attendanceModel.MonthYear on da.mth_yr_id equals my.mth_yr_id
                    where my.month_id == "M05"
                    where sc.teacher_id == "T1000001"
                    select new { DailyAttendance = da, Student = s };


        return View(model);
    }
}

And this is how I tried to view the data

@model IEnumerable<titas.Models.DailyAttendance>

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.student_id)
    <td>
</tr>
}

And the error I am getting is

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[<>f__AnonymousType5`2[titas.Models.DailyAttendance,titas.Models.Student]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[titas.Models.DailyAttendance]'. 
  • 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-03T09:28:10+00:00Added an answer on June 3, 2026 at 9:28 am

    As in every ASP.NET MVC application, define a view model that will contain all the information that you need to work with in your view:

    public class MyViewModel
    {
        public DailyAttendance DailyAttendance { get; set; }
        public Student Student { get; set; }
    }
    

    and then have your controller action return this view model to the view:

    public class AttendanceController : Controller
    {
        TitasDb _attendanceModel = new TitasDb();
    
        public ActionResult Index()
        {
            var model = from s in _attendanceModel.Student
                        join ssc in _attendanceModel.StudentSchoolClass on s.student_id equals ssc.student_id
                        join sc in _attendanceModel.SchoolClass on ssc.school_class_id equals sc.school_class_id
                        join da in _attendanceModel.DailyAttendance on s.student_id equals da.student_id
                        join my in _attendanceModel.MonthYear on da.mth_yr_id equals my.mth_yr_id
                        where my.month_id == "M05"
                        where sc.teacher_id == "T1000001"
                        select new MyViewModel
                        { 
                            DailyAttendance = da, 
                            Student = s 
                        };
            return View(model.ToList());
        }
    }
    

    and finally have your view strongly typed to the view model:

    @model IList<MyViewModel>
    <table>
        <thead>
            <tr>
                <th>student id</th>
                <th>attendance id</th>
            </tr>
        </thead>
        <tbody>
            @for (var i = 0; i < Model.Count; i++)
            {
                <tr>
                    <td>@Html.DisplayFor(x => x[i].Student.student_id)</td>
                    <td>@Html.DisplayFor(x => x[i].DailyAttendance.attendance_id)</td>
                </tr>
            }
        </tbody>
    </table>
    

    and to cleanup the view you could use a display template that will automatically be rendered for each element of the model (~/Views/Shared/DisplayTemplates/MyViewModel.cshtml):

    @model MyViewModel
    <tr>
        <td>@Html.DisplayFor(x => x.Student.student_id)</td>
        <td>@Html.DisplayFor(x => x.DailyAttendance.attendance_id)</td>
    </tr>
    

    and now your main view simply becomes:

    @model IList<MyViewModel>
    <table>
        <thead>
            <tr>
                <th>student id</th>
                <th>attendance id</th>
            </tr>
        </thead>
        <tbody>
            @Html.DisplayForModel()
        </tbody>
    </table>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to retrieve some rows from the database using simple SELECT statement
I'm trying to randomly grab about 20 random rows from a table, then using
I am using SQL Server 2008. Is there a way to select from multiple
After dynamically creating a client-side table based on SELECT information retrieved from MySql database,
i am trying to select information from my mysql database with this statement after
I'm trying to get data from the database in order to create a list
I am trying to store xml data in my database table using a stored
I'm trying to SELECT fields with multiple dots ( . ) in their value.
When trying to select fields from a subquery if ANY of the subqueries do
I'm trying to SELECT based on a date in OpenOffice Base: SELECT * FROM

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.