-2

I have a SQL function that needs to be converted to C#. The SQL function uses the LIKE keyword and the wildcard '_'. How can I write the equivalent C# function? Here is the SQL function:

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
rein
  • 32,967
  • 23
  • 82
  • 106
Shon.Su
  • 111
  • 1
  • 6

1 Answers1

3

I wrote this directly in the comment box (so this function may not compile) but you should use regular expressions. Something similar to this should work:

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;
}
rein
  • 32,967
  • 23
  • 82
  • 106