在我的程序中,我正在阅读excel表并正在做一些linq选择,这工作正常.
问题:我尝试通过应用If语句进行预选.变量将在每种情况下分配(if/else),但编译器没有看到.编译器告诉我初始化var beforehands但是当我尝试这个时,我失败了因为我只习惯像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
}
Run Code Online (Sandbox Code Playgroud)
当我尝试这样做时:
var excel = new ExcelQueryFactory("list.xlsx");ter code here
var discounts = excel.Worksheet();
if (onlyAcceptDiscountedItems == true)
{
discounts = from …Run Code Online (Sandbox Code Playgroud)