ahmed salah

ahmed salah

  • 1.2k
  • 547
  • 66.1k

How to convert this condition to sql server syntax by csharp function

Jul 26 2023 1:59 AM

I work on some ui web tool on asp.net core blazor

i need to write csharp function convert this conditions to sql statement so

when i filter two columns as databasename and remarks

(databaseName == null ? "" : databaseName).ToLower().Contains("db_".ToLower()) and (remarks == null ? "" : remarks).ToLower().Contains("adc".ToLower())

so csharp function will convert text received to sql statement

databaseName like '%db_%' and Remarks like '%adc%'

so csharp function will return string and it will take only one parameters text

as

(databaseName == null ? "" : databaseName).ToLower().Contains("db_".ToLower()) and (remarks == null ? "" : remarks).ToLower().Contains("adc".ToLower())

and it will return

databaseName like '%db_%' and Remarks like '%adc%'

i need to do it with dynamic way so may be it have one column filter may be 2 columns or 3 column filter

then convert it to sql statement

 

what i try but not give me exact result i need

   public  string ExtractKeyLikeValues(string input)
        {
            var pairs = new List<string>();
            string[] substrings = input.Split(new[] { "Contains(" }, StringSplitOptions.None);

            foreach (string substring in substrings)
            {
                int index = substring.IndexOf(".ToLower()");

                if (index != -1)
                {
                    string key = substring.Substring(0, index).Trim().Split(' ')[0];
                    string value = substring.Substring(index + ".ToLower()".Length).Trim().TrimEnd(')');

                    pairs.Add($"{key} like '{value}'");
                }
            }

            return string.Join(" and ", pairs);
        }

 


Answers (2)