我在语句中定义一个变量if
,然后尝试在该语句的上下文之外使用该变量。如何dbSet
在 if 语句之外初始化一个空的,以便viewModel
可以看到/填充它?
这就是我想要完成的事情:
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;
};