U-SQL 中的 int.TryParse

我正在尝试验证 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# 错误 CS1003:语法错误,',' 预期


在标记“_pers”处,### 附近的第 108 行:


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


梵蒂冈之花
浏览 145回答 2
2回答

繁华开满天机

我设法写了一些有用的东西,并根据您的输入将空字符串和默认值替换为超出范围的数字。这是最终代码:DECLARE @defaultPersons int = 2;@forDecisionTreeHouseTypeReplNulls =&nbsp; &nbsp; SELECT&nbsp; [InstallationId],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [Persons],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (Func<string, int?>)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (inputString => // input_parameter&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int _pers;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ! int.TryParse([Persons], out _pers) || _pers <= 0 ?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @defaultPersons :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _pers > 10 ?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 10 :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _pers;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;) ([Persons]) AS [AdjPersons]

ibeautiful

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;
打开App,查看更多内容
随时随地看视频慕课网APP