在c#中指定if块中的条件时是否无法访问代码错误

时间:2018-06-17 08:20:53

标签: c#

我需要在c#中返回块中的条件和条件。我得到一个编译时错误,说无法访问的代码。如果你注意到,我传递参数showCommercialOption。我需要实现if(!showCommercialOption)然后不显示商业部分。我如何实现这一目标?

 private static Models.NpvResults BuildNpvResult(NpvResults npvResults, bool showCommercialOption)
        {
            return new Models.NpvResults
            {
                //Commercial
                CommInsYear = npvResults.CommIns.Select(x => x.year).ToArray(),
                CommInsPremiumPaid = npvResults.CommIns.Select(x => x.premPaid).ToArray(),
                CommInsTaxDeduction = npvResults.CommIns.Select(x => x.taxDeduction).ToArray(),
                CommInsDiscountedTaxDeduction = npvResults.SelfIns.Select(x => x.discountedTaxDeduction).ToArray(),
                CommInsDiscountedLossesPaid = npvResults.SelfIns.Select(x => x.discountedLossesPaid).ToArray(),
                CommInsGraphData = npvResults.GraphData.CommIns.ToArray(),

                //SelfIns
                SelfInsYear = npvResults.SelfIns.Select(x => x.year).ToArray(),
                SelfInsDiscountedLossesPaid = npvResults.SelfIns.Select(x => x.discountedLossesPaid).ToArray(),
                SelfInsDiscountedTaxDeduction = npvResults.SelfIns.Select(x => x.discountedTaxDeduction).ToArray(),
                SelfInsGraphData = npvResults.GraphData.SelfIns.ToArray(),
                //Captive
                CaptiveInsYear = npvResults.CaptiveIns.Select(x => x.year).ToArray(),
                CaptiveInsPremiumPaid = npvResults.CaptiveIns.Select(x => x.premPaid).ToArray(),
                CaptiveInsTaxDeduction = npvResults.CaptiveIns.Select(x => x.taxDeduction).ToArray(),
                CaptiveInsLoanToParent = npvResults.CaptiveIns.Select(x => x.loanToParent).ToArray(),
                CaptiveInsCapitalContribution = npvResults.CaptiveIns.Select(x => x.capContriDistr).ToArray(),
                CaptiveDividentDistribution = npvResults.CaptiveIns.Select(x => x.dividendDistr).ToArray(),
                CaptiveInsTerminalValue = npvResults.CaptiveIns.Select(x => x.terminalVal).ToArray(),
                CaptiveInsGraphData = npvResults.GraphData.CaptiveIns.ToArray()
            };
        }

2 个答案:

答案 0 :(得分:3)

一个简单的解决方案是将其拆分为对象初始值设定项和if块。这需要一个中间变量......

private static Models.NpvResults BuildNpvResult(NpvResults npvResults, bool showCommercialOption)
{
    var m = new Models.NvpResults()
    {
        //SelfIns
        SelfInsYear = npvResults.SelfIns.Select(x => x.year).ToArray(),
        SelfInsDiscountedLossesPaid = npvResults.SelfIns.Select(x => x.discountedLossesPaid).ToArray(),
        SelfInsDiscountedTaxDeduction = npvResults.SelfIns.Select(x => x.discountedTaxDeduction).ToArray(),
        SelfInsGraphData = npvResults.GraphData.SelfIns.ToArray(),
        //Captive
        CaptiveInsYear = npvResults.CaptiveIns.Select(x => x.year).ToArray(),
        CaptiveInsPremiumPaid = npvResults.CaptiveIns.Select(x => x.premPaid).ToArray(),
        CaptiveInsTaxDeduction = npvResults.CaptiveIns.Select(x => x.taxDeduction).ToArray(),
        CaptiveInsLoanToParent = npvResults.CaptiveIns.Select(x => x.loanToParent).ToArray(),
        CaptiveInsCapitalContribution = npvResults.CaptiveIns.Select(x => x.capContriDistr).ToArray(),
        CaptiveDividentDistribution = npvResults.CaptiveIns.Select(x => x.dividendDistr).ToArray(),
        CaptiveInsTerminalValue = npvResults.CaptiveIns.Select(x => x.terminalVal).ToArray(),
        CaptiveInsGraphData = npvResults.GraphData.CaptiveIns.ToArray()
    };

    if (showCommercialOption)
    {
        m.CommInsYear = npvResults.CommIns.Select(x => x.year).ToArray();
        m.CommInsPremiumPaid = npvResults.CommIns.Select(x => x.premPaid).ToArray();
        m.CommInsTaxDeduction = npvResults.CommIns.Select(x => x.taxDeduction).ToArray();
        m.CommInsDiscountedTaxDeduction = npvResults.SelfIns.Select(x => x.discountedTaxDeduction).ToArray();
        m.CommInsDiscountedLossesPaid = npvResults.SelfIns.Select(x => x.discountedLossesPaid).ToArray();
        m.CommInsGraphData = npvResults.GraphData.CommIns.ToArray();
     }

     return m;
}

答案 1 :(得分:0)

塞巴斯蒂安已经充分回答了这个问题。但是你可以使用conditional operator,虽然这是非常臭的IMO

  

条件运算符(?:),通常称为三元运算符   条件运算符,根据值返回两个值之一   一个布尔表达式。

return new Models.NpvResults
   {
      //Commercial
      CommInsYear = !showCommercialOption ? null : npvResults.CommIns.Select(x => x.year).ToArray(),
      CommInsPremiumPaid = !showCommercialOption ? null : npvResults.CommIns.Select(x => x.premPaid).ToArray(),
      CommInsTaxDeduction = !showCommercialOption ? null : npvResults.CommIns.Select(x => x.taxDeduction).ToArray(),
      CommInsDiscountedTaxDeduction = !showCommercialOption ? null : npvResults.SelfIns.Select(x => x.discountedTaxDeduction).ToArray(),
      CommInsDiscountedLossesPaid = !showCommercialOption ? null : npvResults.SelfIns.Select(x => x.discountedLossesPaid).ToArray(),
      CommInsGraphData = !showCommercialOption ? null : npvResults.GraphData.CommIns.ToArray(),

      //SelfIns
      SelfInsYear = npvResults.SelfIns.Select(x => x.year).ToArray(),
      SelfInsDiscountedLossesPaid = npvResults.SelfIns.Select(x => x.discountedLossesPaid).ToArray(),
      SelfInsDiscountedTaxDeduction = npvResults.SelfIns.Select(x => x.discountedTaxDeduction).ToArray(),
      SelfInsGraphData = npvResults.GraphData.SelfIns.ToArray(),
      //Captive
      CaptiveInsYear = npvResults.CaptiveIns.Select(x => x.year).ToArray(),
      CaptiveInsPremiumPaid = npvResults.CaptiveIns.Select(x => x.premPaid).ToArray(),
      CaptiveInsTaxDeduction = npvResults.CaptiveIns.Select(x => x.taxDeduction).ToArray(),
      CaptiveInsLoanToParent = npvResults.CaptiveIns.Select(x => x.loanToParent).ToArray(),
      CaptiveInsCapitalContribution = npvResults.CaptiveIns.Select(x => x.capContriDistr).ToArray(),
      CaptiveDividentDistribution = npvResults.CaptiveIns.Select(x => x.dividendDistr).ToArray(),
      CaptiveInsTerminalValue = npvResults.CaptiveIns.Select(x => x.terminalVal).ToArray(),
      CaptiveInsGraphData = npvResults.GraphData.CaptiveIns.ToArray()
   };