1

我有以下语句作为为数据表构建数据行的一部分,我想知道是否可以使用 lambda 语句或更优雅的方法来缩短它。

if (outval(line.accrued_interest.ToString()) == true) 
{ 
temprow["AccruedInterest"] = line.accrued_interest; 
} 
else 
{
temprow["AccruedInterest"] = DBNull.Value;
}

该语句由以下人员检查:

 public static bool outval(string value)
        {
            decimal outvalue;
            bool suc = decimal.TryParse(value, out outvalue);
            if (suc)
            {
                return true;
            }
            else
            {
                return false;
            }


        }
4

4 回答 4

3

你想要吗?运算符,您不需要 lambda 表达式。

http://msdn.microsoft.com/en-us/library/ty67wk28.aspx

int input = Convert.ToInt32(Console.ReadLine());
string classify;

// if-else construction.
if (input < 0)
    classify = "negative";
else
    classify = "positive";

// ?: conditional operator.
classify = (input < 0) ? "negative" : "positive";
于 2013-08-14T16:09:56.740 回答
3
public static bool outval(string value)
{
    decimal outvalue;
    return decimal.TryParse(value, out outvalue);
}

temprow["AccruedInterest"] = outval(line.accrued_interest.ToString()) ? (object)line.accrued_interest : (object)DBNull.Value;

编辑: 转换为object很重要,因为?:三元运算符需要返回结果,真案例和假案例都必须隐式转换为其他。我不知道什么是类型,accrued_interest我假设它是 a doubleor因为anddecimal之间没有隐式转换。为了使其工作,您必须强制转换为类型。明白了吗?decimalDBNullobject

于 2013-08-14T16:05:03.457 回答
0

您不需要调用单独的方法。不需要方法或任何其他东西

decimal result;   
if(decimal.TryParse(line.accrued_interest.ToString(),out result))
 temprow["AccruedInterest"] = line.accrued_interest
else
 temprow["AccruedInterest"] = DBNull.Value; 
于 2013-08-14T16:09:30.060 回答
0

还,

public static bool outval(string value)
{
    decimal outvalue;
    bool suc = decimal.TryParse(value, out outvalue);
    if (suc)
    {
        return true;
    }
    else
    {
        return false;
    }
}

至..

public static bool outval(string value)
{
    decimal outvalue;
    return decimal.TryParse(value, out outvalue);
}
于 2015-06-07T17:18:34.937 回答