int.TryParse 在 U-SQL

int.TryParse in U-SQL

我正在尝试验证 U-SQL SELECT 中的字符串变量是否可以解释为整数,因此我正在尝试使用 int.TryParse 替换“0 " 和 "" 的默认值为 2,所有大于 10 的都为 10。代码如下:

DECLARE @maxAvgkWh double = 100.00;
DECLARE @defaultM2 int = 90;
DECLARE @defaultPersons int = 2;


// Extracting installations and their information
@forDecisionTree =
    EXTRACT [InstallationId] string,
            [PrimaryHeatingType] string,
            [Persons] string,
            [SquareMeters] string,
            [LatestAvgDailykWh] double
    FROM "adl://some text file in azure data lake"
    USING Extractors.Tsv(skipFirstNRows : 1, silent : true);


// Making sure that NULLS and zeroes and abnormal values are replaced with default values
@forDecisionTreeHouseTypeReplNulls =
    SELECT  [InstallationId],
            [PrimaryHeatingType],
            (
                ! int.TryParse(Persons, out var _pers) || _pers <= 0 ?  
                    @defaultPersons :
                    _pers > 10 ?
                        10 :
                        _pers
            ).ToString() AS [Persons],
            (
                ! int.TryParse([SquareMeters], out var _m2) || _m2 <= 0 ?  
                    @defaultM2 :
                    _m2 > 500 ?
                        500 :
                        _m2
            ).ToString() AS [SquareMeters],
            [LatestAvgDailykWh]
    FROM @forDecisionTreeHouseType
    WHERE [LatestAvgDailykWh] < @maxAvgkWh;

我不断收到以下错误:

C# error CS1003: Syntax error, ',' expected

at token '_pers', line 108 near the ###:


...! int.TryParse([Persons], out var ### _pers) || _pers <= 0 ? ...

TryParse 不是您可以直接调用的函数之一。它必须包装为内联函数。一个简单的例子:

@output =
    SELECT FirstName,
               (
                (Func<string, int?>)
                (inputString =>  // input_paramater
                    { 
                        int outputValue;
                        return int.TryParse(inputString, out outputValue) ? (int?)outputValue : (int?)null;
                    }
                 )
            ) (Salary) AS someDate

    FROM @Employees;

例子here and here.

感谢 wBob 的输入。我设法写了一些有用的东西,并根据您的输入将空字符串和默认值替换为超出范围的数字。这是最终代码:

DECLARE @defaultPersons int = 2;

@forDecisionTreeHouseTypeReplNulls =
    SELECT  [InstallationId],
            [Persons],
                (   
                (Func<string, int?>)
                (inputString => // input_parameter
                    {
                        int _pers;
                        return ! int.TryParse([Persons], out _pers) || _pers <= 0 ?
                            @defaultPersons :
                            _pers > 10 ?
                                10 :
                                    _pers;
                    }
                )
             ) ([Persons]) AS [AdjPersons]