我在if
語句中定義了一個變量,然後嘗試在該語句的上下文之外使用該變量。如何在if語句之外初始化空的dbSet
以便viewModel
可以看到/填充它?初始化ASP.NET MVC中的空白dbSet 3
這就是我試圖完成:
if (order.type.ToString() == "Rush")
{
var filteredOrders = db.Orders.Where(a => a.rushID == order.rushID).ToList();
}
else if (order.type.ToString() == "Standard")
{
var filteredOrders = db.Orders.Where(a => a.standardID == order.standardID).ToList();
}
else
{
return HttpNotFound();
}
var viewModel = new OrderDetailsViewModel
{
PastOrders = filteredOrders, // filteredOrders doesn't exist here
Order = order;
};
您正在使用您的查詢的.ToList()方法。你爲什麼不在if語句之外定義'IList filteredOrders'? –
juanreyesv