如何使用 Entity Framework 连接两个表
How to join two tables using Entity Framework
控制器:
public ActionResult Index()
{
var Cs = new List<Customer>();
using (JoinEntities db = new JoinEntities())
{
Cs = (from p in db.Customers
join e in db.Orders on p.ID equals e.Customer_id
where p.Name == "Rama"
select p).ToList();
}
return View(Cs);
}
查看:
@model IEnumerable<JOIN.Models.Customer>
@{
ViewBag.Title = "Home Page";
}
<table class="table table-condensed table-hover">
<thead>
<tr>
<td>First Name</td>
<td>salary</td>
<td>age</td>
<td>amount</td>
</tr>
</thead>
<tbody>
@foreach (var per in Model)
{
<tr>
<td>@per.ID</td>
<td>@per.Name</td>
<td>@per.Age</td>
<td>@per.Amount</td>
</tr>
}
</tbody>
</table>
上面的代码视图是否只包含一个 table 列 我怎样才能获得 customer table 中的其他 table 列 id 是主键和订单 table customer_id
是外键
customer
table: id, name, age, salary 列
order
table: oid, date, customer_id, amount columns
您需要创建另一个模型,例如
class MyModel{
public string Name{get;set;}
public DateTime Date {get;set;}
}
并在 select 查询中更改:
var Cs = new List<MyModel>();
using (JoinEntities db = new JoinEntities())
{
Cs = (from p in db.Customers
join e in db.Orders on p.ID equals e.Customer_id
where p.Name == "Rama"
select new MyModel {
Name = p.Name,
Date = e.date
}).ToList();
}
如下创建另一个视图模型
public class CustomerOrderViewModel
{
public int CustomerId { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public double Salary { get; set; }
public int OrderId { get; set; }
public DateTime Date { get; set; }
public double Amount { get; set; }
}
并如下更新您的 Index 方法
public ActionResult Index()
{
using (JoinEntities db = new JoinEntities())
{
var customerOrderList = (from p in db.Customers
join e in db.Orders on p.ID equals e.Customer_id
where p.Name == "Rama"
select new CustomerOrderViewModel {
CustomerId = p.Id,
Name = p.Name,
Age= p.Age,
Salary = p.Salary,
OrderId= e.Id,
Date= e.Date,
Amount = e.Amount
}).ToList();
return View(customerOrderList);
}
}
并将视图的视图模型更改为
@model IEnumerable<JOIN.Models.CustomerOrderViewModel>
控制器:
public ActionResult Index()
{
var Cs = new List<Customer>();
using (JoinEntities db = new JoinEntities())
{
Cs = (from p in db.Customers
join e in db.Orders on p.ID equals e.Customer_id
where p.Name == "Rama"
select p).ToList();
}
return View(Cs);
}
查看:
@model IEnumerable<JOIN.Models.Customer>
@{
ViewBag.Title = "Home Page";
}
<table class="table table-condensed table-hover">
<thead>
<tr>
<td>First Name</td>
<td>salary</td>
<td>age</td>
<td>amount</td>
</tr>
</thead>
<tbody>
@foreach (var per in Model)
{
<tr>
<td>@per.ID</td>
<td>@per.Name</td>
<td>@per.Age</td>
<td>@per.Amount</td>
</tr>
}
</tbody>
</table>
上面的代码视图是否只包含一个 table 列 我怎样才能获得 customer table 中的其他 table 列 id 是主键和订单 table customer_id
是外键
customer
table: id, name, age, salary 列order
table: oid, date, customer_id, amount columns
您需要创建另一个模型,例如
class MyModel{
public string Name{get;set;}
public DateTime Date {get;set;}
}
并在 select 查询中更改:
var Cs = new List<MyModel>();
using (JoinEntities db = new JoinEntities())
{
Cs = (from p in db.Customers
join e in db.Orders on p.ID equals e.Customer_id
where p.Name == "Rama"
select new MyModel {
Name = p.Name,
Date = e.date
}).ToList();
}
如下创建另一个视图模型
public class CustomerOrderViewModel
{
public int CustomerId { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public double Salary { get; set; }
public int OrderId { get; set; }
public DateTime Date { get; set; }
public double Amount { get; set; }
}
并如下更新您的 Index 方法
public ActionResult Index()
{
using (JoinEntities db = new JoinEntities())
{
var customerOrderList = (from p in db.Customers
join e in db.Orders on p.ID equals e.Customer_id
where p.Name == "Rama"
select new CustomerOrderViewModel {
CustomerId = p.Id,
Name = p.Name,
Age= p.Age,
Salary = p.Salary,
OrderId= e.Id,
Date= e.Date,
Amount = e.Amount
}).ToList();
return View(customerOrderList);
}
}
并将视图的视图模型更改为
@model IEnumerable<JOIN.Models.CustomerOrderViewModel>