Scenario:
Parent-child relationship between CustomerOrders & CustomerOrderItems in db.
Both CustomerOrders and CustomerOrderItems are defined in the auto-generated Linq2Sql dbml class.
MVC Code:
I have a view model class:
public class OrdersListViewModel
{
public IEnumerable<CustomerOrder> CustomerOrders { get; set; }
public PagingInfo PagingInfo { get; set; }
}
which passes data to a partial view (only interested in the CustomerOrders data):
@model CallTracking.Domain.Concrete.CustomerOrder
<tr>
<td>@Model.Auto_ID</td>
<td>@Model.OrderTotal</td>
<td>@string.Format("{0} {1}", Model.CustomerFirstName, Model.CustomerSecondName)</td>
<td>@Model.Email</td>
**<td>@Model.CustomerOrderItems.OrderByDescending(i => i.SalesPrice).FirstOrDefault().ItemName</td>**
</tr>
This works fine, the problem is that I’ve used a Linq expression in order to display the highest value orderitem for each order record, but I feel (and have read) that I shouldn’t use Linq in the view and this should be a property in the view model i.e.
public CustomerOrderItem HighestValueOrderItem { get; set; }
It’s at this point where I get stuck – how & where would I set this value?
In CustomerOrder Model Create Property Called