在 if 语句之前初始化一个对象
Initialize an object before an if statement
在我的程序中,我正在阅读 excel sheet 并正在做一些 linq 选择,效果很好。
问题:我试图通过应用 If 语句进行预选。在每种情况下都会分配变量 (if/else),但编译器看不到。编译器告诉我预先初始化 var,但是当我尝试这样做时,我失败了,因为我只习惯于像 sting、int 或 double 这样的变量,我可以轻松地预先分配这些变量:
//This function takes the downloaded xlsx and makes selection of applicable items
var excel = new ExcelQueryFactory("list.xlsx");
//get all items with discount
if (onlyAcceptDiscountedItems == true)
{
var discounts = from s in excel.Worksheet()
where s["Discount/Premium"].Cast<string>().StartsWith("-")
select s;
}
else
{
var discounts = excel.Worksheet();
}
if (discounts.Count() != 0)
{
//some application logic comes here
}
当我尝试这样做时:
var excel = new ExcelQueryFactory("list.xlsx");ter code here
var discounts = excel.Worksheet();
if (onlyAcceptDiscountedItems == true)
{
discounts = from s in excel.Worksheet()
where s["Discount/Premium"].Cast<string>().StartsWith("-")
select s;
}
if (discounts.Count() != 0)
{
//some application logic comes here
}
我在第二个代码片段的第 5 行收到以下错误:
Error CS0266 Cannot implicitly convert type
'System.Linq.IQueryable' to
'LinqToExcel.Query.ExcelQueryable'. An explicit
conversion exists (are you missing a cast?)
我看到三个选项,尽管还有其他选项:
您可以显式声明您想要的变量类型,在这种情况下您可以在每种情况下单独初始化它:
IQueryable<LinqToExcel.Row> discounts;
if (onlyAcceptDiscountedItems == true)
{
discounts = from s in excel.Worksheet()
where s["Discount/Premium"].Cast<string>().StartsWith("-")
select s;
}
else
{
discounts = excel.Worksheet();
}
您可以使用条件运算符来初始化变量:
var discounts = onlyAcceptDiscountedItems
? excel.Worksheet().Where(s => s["Discount/Premium"].Cast<string>().StartsWith("-"))
: excel.Worksheet();
您可以在两个地方删除调用 excel.Worksheet
的冗余:
IQueryable<LinqToExcel.Row> discounts = excel.Worksheet();
if (onlyAcceptDiscountedItems)
{
discounts = discounts.Where(s => s["Discount/Premium"].Cast<string>().StartsWith("-"));
}
此外,您可能希望使用 discounts.Any()
而不是 discounts.Count() > 0
,因为您并不真正关心计数。
在我的程序中,我正在阅读 excel sheet 并正在做一些 linq 选择,效果很好。
问题:我试图通过应用 If 语句进行预选。在每种情况下都会分配变量 (if/else),但编译器看不到。编译器告诉我预先初始化 var,但是当我尝试这样做时,我失败了,因为我只习惯于像 sting、int 或 double 这样的变量,我可以轻松地预先分配这些变量:
//This function takes the downloaded xlsx and makes selection of applicable items
var excel = new ExcelQueryFactory("list.xlsx");
//get all items with discount
if (onlyAcceptDiscountedItems == true)
{
var discounts = from s in excel.Worksheet()
where s["Discount/Premium"].Cast<string>().StartsWith("-")
select s;
}
else
{
var discounts = excel.Worksheet();
}
if (discounts.Count() != 0)
{
//some application logic comes here
}
当我尝试这样做时:
var excel = new ExcelQueryFactory("list.xlsx");ter code here
var discounts = excel.Worksheet();
if (onlyAcceptDiscountedItems == true)
{
discounts = from s in excel.Worksheet()
where s["Discount/Premium"].Cast<string>().StartsWith("-")
select s;
}
if (discounts.Count() != 0)
{
//some application logic comes here
}
我在第二个代码片段的第 5 行收到以下错误:
Error CS0266 Cannot implicitly convert type 'System.Linq.IQueryable' to 'LinqToExcel.Query.ExcelQueryable'. An explicit conversion exists (are you missing a cast?)
我看到三个选项,尽管还有其他选项:
您可以显式声明您想要的变量类型,在这种情况下您可以在每种情况下单独初始化它:
IQueryable<LinqToExcel.Row> discounts;
if (onlyAcceptDiscountedItems == true)
{
discounts = from s in excel.Worksheet()
where s["Discount/Premium"].Cast<string>().StartsWith("-")
select s;
}
else
{
discounts = excel.Worksheet();
}
您可以使用条件运算符来初始化变量:
var discounts = onlyAcceptDiscountedItems
? excel.Worksheet().Where(s => s["Discount/Premium"].Cast<string>().StartsWith("-"))
: excel.Worksheet();
您可以在两个地方删除调用 excel.Worksheet
的冗余:
IQueryable<LinqToExcel.Row> discounts = excel.Worksheet();
if (onlyAcceptDiscountedItems)
{
discounts = discounts.Where(s => s["Discount/Premium"].Cast<string>().StartsWith("-"));
}
此外,您可能希望使用 discounts.Any()
而不是 discounts.Count() > 0
,因为您并不真正关心计数。