【问题标题】:Northwind database JOIN QUERY between three tables (many to many)Northwind 数据库 JOIN QUERY 在三个表之间(多对多)
【发布时间】:2017-10-23 13:59:43
【问题描述】:

我有 Northwind 数据库的三张表,

员工(字段:employeeID)1-*

订单(字段:Orderid,customerid,employeeid)*-1

客户(字段:customerID)。

所以 1 Employee 有很多 Orders 和 1 Customer 有很多 orders ,所以它是 [Employee 1*Order *1 Customer ] 关系,中间有一个订单表。那么我如何提取并在 mvc 视图中显示所有 EMPLOYEES THAT HAVE CUSTOMERS

【问题讨论】:

    标签: sql linq join model-view-controller


    【解决方案1】:

    首先,我们假设每个订单都有一个客户,所以如果一个员工有一个订单,他也必须有一个客户。所以,我们可以忘记客户表。我们真的只想知道,哪些员工有订单。为此,我们查看订单并收集拥有它们的员工列表:

      (from o in Orders
      select o.Employee).Distinct()
    

    【讨论】:

    • So this is a part of how view looks like: @model Northwind.Employees @{ ViewBag.Title = "Me"; Layout = "~/Views/Shared/MasterDetailsLayoutPage.cshtml"; } @Html.DisplayNameFor(model => model.EmployeeID) @Html.DisplayFor(model => model.EmployeeID) ........AND SOME OTHER EMPLOYEES FIELD.... THEN HERE display a table: foreach(item in Model.Customers) .....display a table of customers for this employee(example EMPLOYEEID:5 ). This is the controller : dbNorthwindEntities db = new dbNorthwindEntities(); /* public ActionResult Details() { //query comes here return View(); }*/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 2014-09-29
    • 1970-01-01
    • 2017-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多