在switch
中声明一些变量并在外部使用它。
我不知道语法。
为了解决我的问题,我想做这样的事情,但是不行,因为它只是一种哲学:
public ActionResult Compare(int id, string orderBy)
{
var productsList = Products.BuildIndividualProductComparisonList(id);
var product = Products.BuildToCompare(id);
switch (orderBy)
{
case "lowestToBiggest":
var organizedProductsList =
productsList.OrderBy(x => x.minProductPrice);
break;
case "biggestToLowest":
var organizedProductsList =
productsList.OrderBy(x => x.maxProductPrice);
break;
default:
var organizedProductsList =
productsList.OrderBy(x => x.minProductPrice);
break;
}
ComparisonViewModel comparisonViewModel =
new ComparisonViewModel
{
Product = product,
ProductList = organizedProductsList
};
return View(comparisonViewModel);
}
这是我原来的C#代码,效果很好:
public ActionResult Compare(int id, string orderBy)
{
var productsList = Products.BuildIndividualProductComparisonList(id);
var product = Products.BuildToCompare(id);
ComparisonViewModel comparisonViewModel =
new ComparisonViewModel
{
Product = product,
ProductList = productsList
};
return View(comparisonViewModel);
}
有人有任何想法解决我的问题吗?
答案 0 :(得分:7)
在交换机中声明一些变量并在外部使用它。
您无法。范围内定义的变量只能在该范围内可见。
您必须在switch
语句之外声明您的变量,然后您才能在外部使用它。
我发现您使用的是var
(隐式类型变量),并且您无法在switch
语句之外声明它(因为需要分配)< / em>,您应该看到:Declaring an implicitly typed variable inside conditional scope and using it outside和answer from Eric Lippert
答案 1 :(得分:5)
在switch块外声明变量并为其赋值。另请尽量不要将List
后缀用于集合名称:
IEnumerable<Product> sortedProducts;
switch (orderBy)
{
case "lowestToBiggest":
sortedProducts = products.OrderBy(x => x.minProductPrice);
break;
case "biggestToLowest":
sortedProducts = products.OrderBy(x => x.maxProductPrice);
break;
default:
sortedProducts = products.OrderBy(x => x.minProductPrice);
break;
}
实际上您的代码可以简化为:
IEnumerable<Product> sortedProducts =
products.OrderBy(p => orderBy == "biggestToLowest" ?
p.maxProductPrice :
p.minProductPrice);
答案 2 :(得分:1)
只需将变量的声明放在switch
块之外。
但是,您必须在声明时指定变量的类型,而不是使用var
,因为无法推断类型。
public ActionResult Compare(int id, string orderBy)
{
var productsList = Products.BuildIndividualProductComparisonList(id);
var product = Products.BuildToCompare(id);
var organizedProductsList = null;
switch (orderBy)
{
case "lowestToBiggest":
organizedProductsList =
productsList.OrderBy(x => x.minProductPrice);
break;
case "biggestToLowest":
organizedProductsList =
productsList.OrderBy(x => x.maxProductPrice);
break;
default:
organizedProductsList =
productsList.OrderBy(x => x.minProductPrice);
break;
}
ComparisonViewModel comparisonViewModel =
new ComparisonViewModel
{
Product = product,
ProductList = organizedProductsList
};
return View(comparisonViewModel);
}
答案 3 :(得分:0)
您不能引用其封闭范围之外的变量。
或者:
我认为选项1是显而易见的。要扩展选项2,代码将如下所示:
ProductList organizedProductsList(...)
{
return productsList.OrderBy(...);
}
....
ComparisonViewModel comparisonViewModel =
new ComparisonViewModel
{
Product = product,
ProductList = organizedProductsList(...)
};
答案 4 :(得分:0)
您需要在case语句之外声明变量 organizedProductsList ,因为它只包含case语句的范围。
注意:我不知道Products.BuildIndividualProductComparisonList(id)返回什么,我推测它是一个产品列表。
因此像:
IEnumerable<Products> organizedProductsList = null;
switch (orderBy)
{
case "lowestToBiggest":
organizedProductsList =
productsList.OrderBy(x => x.minProductPrice);
break;
case "biggestToLowest":
organizedProductsList =
productsList.OrderBy(x => x.maxProductPrice);
break;
default:
organizedProductsList =
productsList.OrderBy(x => x.minProductPrice);
break;
}