简体   繁体   English

如何将联接SQL查询表结果传递给另一种方法?

[英]How to pass join sql query table result to another method?

I am working in ASP.NET MVC using Entity Framework. 我正在使用实体框架在ASP.NET MVC中工作。 I have two tables, AppointmentDb and StudentDb in my db. 我的数据库中有两个表AppointmentDbStudentDb I wish to join them in the controller and pass the result to another action method within the controller. 我希望将它们加入控制器中,并将结果传递给控制器​​中的其他操作方法。 What is the return type of this new table that I pass in Details action? 我在“ Details操作中传递的此新表的返回类型是什么?

public ActionResult Index(AdminViewModel model)
{
    if (ModelState.IsValid)
    {              
        var innerJoinQuery = from appointment in AppointmentDb
                             join student in StudentDb on appointment.StudentFirstName equals student.FirstName
                             select appointment;
        return View("Details",innerJoinQuery);
    }
}

public ActionResult Details(?????? innerJoinQuery)
{           
    return View();
}

Edit 1: 编辑1:

From the answers below, I edited to: 从下面的答案中,我编辑为:

    public ActionResult Details(IQueryable<Appointment> appointment)
    {

        return View(appointment);
    }

I am still unable to call the columns as desired below. 我仍然无法根据需要在下面调用这些列。 Some of them like AppointmentID are from AppointmentDb and the rest are from StudentDb Below is the View Details.cshtml : 其中一些像AppointmentID来自AppointmentDb ,其余则来自StudentDb下面是View Details.cshtml

@model IQueryable<OdeToFood.Entities.Appointment>

@{
    ViewBag.Title = "Details";
}

<h2>Index</h2>
@Html.ActionLink("Back to Search", "Index")
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.AppointmentID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.StudentFirstName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.StudentID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.AppointmentDate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.AppointmentTime)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Status)
        </th>
    </tr>
</table>

How should I make this View work? 我应该如何使该View工作?

Edit 2: 编辑2:

I have made the edits as suggested in Edit 1. There is yet another small error in my complete View.cshtml . 我已经按照编辑1中的建议进行了编辑。完整的View.cshtml还有另一个小错误。 This is the error I receive foreach cannot operate on Variables of type OdeToFood.ViewModels.AdminDetailsViewModel because it does not contain definition for GetEnumerator 这是我收到的foreach cannot operate on Variables of type OdeToFood.ViewModels.AdminDetailsViewModel because it does not contain definition for GetEnumerator的错误foreach cannot operate on Variables of type OdeToFood.ViewModels.AdminDetailsViewModel because it does not contain definition for GetEnumerator

@model AdminDetailsViewModel

@{
    ViewBag.Title = "Details";
}

<h2>Index</h2>
@Html.ActionLink("Back to Search", "Index")
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.AppointmentID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.StudentFirstName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.StudentLastName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.StudentID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.AppointmentDate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.AppointmentTime)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Status)
        </th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.AppointmentID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.StudentFirstName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.StudentLastName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.StudentID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.AppointmentDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.AppointmentTime)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Status)
            </td>
        </tr>
    }

</table>

Your query code returns IQueryable<AppointmentDb> : 您的查询代码返回IQueryable<AppointmentDb>

 public ActionResult Details(IQueryable<AppointmentDb> innerJoinQuery)
 {           
        return View();
 }

Approach 2: 方法二:

Better execute your query first: 最好先执行查询:

var innerJoinQuery = (from appointment in AppointmentDb
                     join student in StudentDb 
                     on appointment.StudentFirstName equals student.FirstName
                     select appointment).ToList();

Then pass the resulting list: 然后传递结果列表:

public ActionResult Details(List<AppointmentDb> innerJoinQuery)
{           
    return View();
}

Edit: 编辑:

You need to create a class to hold data from multiple tables: 您需要创建一个类来保存来自多个表的数据:

class MyViewModel
{
    public int AppointmentID { set; get; }
    public string StudentFirstName { set; get; }
    // continue the rest
}

Then change your query to: 然后将查询更改为:

from appointment in AppointmentDb
join student in StudentDb
on appointment.StudentFirstName equals student.FirstName
select new MyViewModel()
{
    AppointmentID = AppointmentDb.AppointmentID,
    StudentFirstName = StudentDb.StudentFirstName
    // continue the rest
};

You can use object type 您可以使用对象类型

public ActionResult Details(object innerJoinQuery)
{           
   return View();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM