Query ?:var customerOrdersQuery = (from c in db.Customers
join o in db.Orders on c.CustomerId equals o.CustomerId
select new
{
CustomerName = c.Name,
OrderDate = o.OrderDate
}).ToList();
Method:var customerOrdersMethod = db.Customers
.Join(
db.Orders, // 1. Table to join
c => c.CustomerId, // 2. Key from the first table (Customers)
o => o.CustomerId, // 3. Key from the second table (Orders)
(c, o) => new // 4. The data you want to select
{
CustomerName = c.Name,
OrderDate = o.OrderDate
}
).ToList();
And then to display : @model IEnumerable
@{
ViewData["Title"] = "Full Order Report";
}
<h2>Customer Order Report</h2>
<table>
<thead>
<tr>
<th>Customer Name</th>
<th>Product Purchased</th>
<th>Date of Order</th>
</tr>
</thead>
<tbody>
@* Loop through the joined data stored in the ViewModels *@
@foreach (var item in Model)
{
<tr>
<td>@item.CustomerName</td>
<td>@item.ProductName</td>
<td>@Html.DisplayFor(modelItem => item.OrderDate)</td>
</tr>
}
</tbody>
</table>
Top comments (0)