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[] 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);
}
ahmed salahPosted Jul 26, 2023, 5:25 AM
thanks for support these values not static as db_ can changed for database name alos remarks adc can changed
so i need way to fill list dynamically '
formate is fixed but some text will be changes as below
(x == null ? "" : x).ToLower().Contains("xx".ToLower()) and (x == null ? "" : x).ToLower().Contains("xx".ToLower())
x represent values key or value will be changed but formte not cha ged
Rajeesh MenothPosted Jul 26, 2023, 5:10 AM
You can create a C# function that takes a list of Filter objects. Try the following code.
Code reference:
https://dotnetfiddle.net/bWjVD1