是否可以對下面的表Timeunit進行LINQ方法查詢並使用外鍵Employee或Order從特定行中獲取數據?LINQ查詢可能使用外鍵從其他表中獲取值
這就是我想要實現的。在View中,我將ViewModel與Timeunit,Employee和Order一起傳遞。在視圖中的訂單的ForEach循環中,我嘗試獲取所有已經處理該訂單的員工。這是可能的還是我在這裏想錯了?
@foreach (var item1 in Model.orderlist)
{
@Model.timeunitlist.Where(x => x.OrderID == item1.EmployeeID).FirstName ????
}
public class Timeunit
{
public int ID { get; set; }
public int Week { get; set; }
public int HoursPerWeek { get; set; }
public int EmployeeID { get; set; }
public int? OrderID { get; set; }
public virtual Employee Employee { get; set; }
public virtual Order Order { get; set; }
}
編輯1:其他兩個表
public class Order
{
public int ID { get; set; }
public string Name { get; set; }
public int? ManufacturerID { get; set; }
public virtual Manufacturer Manufacturer { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
}
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int EmployeeNumber { get; set; }
public virtual ICollection<Timeunit> Timeunits { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}
編輯2:視圖模型
public class ViewModel
{
public List<Timeunit> timeunitList = new List<Timeunit>();
public List<Employee> employeeList = new List<Employee>();
public List<Order> orderList = new List<Order>();
}
我需要的是對每一個訂單,我需要列出所有匹配的Timeunits每個訂單在特定的一週,並顯示每個員工在每個訂單中工作了多少小時
'@ Model.timeunitlist.Where(X => x.OrderID == item1.EmployeeID)。選擇(X => x.Employee.FirstName)'可能工作 – Jens
什麼是你的視圖模型。屬性'orderlist'是'Timeunit'的集合嗎? –
@StephenMuecke我添加了其他兩個表,如果這可能有任何幫助? –