我有一个需要转换为C#的SQL函数。 SQL函数使用LIKE关键字和通配符“_”。如何编写等效的C#函数? 这是SQL函数:
Create Function [dbo].[fnGetWarehouseByGrade222]
(
@Grade nvarchar(2),
@Paste nvarchar(500)
)
Returns varchar(500)
As
Begin
declare @Return varchar(500)
if @Grade = '00' or @Grade = '01' or @Grade = '02' or @Grade = '03' or @Grade = '04'
begin
Select @Return = case when @Paste like 'D__G__DG' then '1GD'
when @Paste like 'D__G__DP' then '1GD'
when @Paste like 'D__G__D_' then '1GO'
when @Paste like 'N__G__D_' then '1GN'
when @Paste like 'D__G__H_' then '1GH'
when @Paste like 'N__G__H_' then '1GM'
when @Paste like 'G__G__H_' then '1GG' end
RETURN @Return
end
RETURN null
End
答案 0 :(得分:3)
我直接在评论框中写了这个(所以这个函数可能无法编译)但你应该使用正则表达式。类似的东西应该有效:
string GetWarehousByGrade222(string grade, string paste)
{
if (grade == "00" || grade == "01" || grade == "02" || grade == "03" || grade == "04") {
if (Regex.IsMatch(paste, "D..G..DG")) return "1GD";
if (Regex.IsMatch(paste, "D..G..DP")) return "1GD";
if (Regex.IsMatch(paste, "D..G..D.")) return "1GO";
// etc...
}
return null;
}