For some reason I need to convert Linq Expression to SQL query condition. This is my code:
public class Program
{
static void Main(string[] args)
{
List<Person> list = new List<Person>();
list.Add(new Person() { Id = 1, Name = "Leo" });
list.Add(new Person() { Id = 2, Name = "Evan" });
list.Add(new Person() { Id = 3, Name = "Diego" });
int Parameter_Id = 1;
string Parameter_Name = "Leo";
string sqlCondition = GetSql(list, (x => x.Name == Parameter_Name || x.Id == Parameter_Id));
Console.WriteLine(sqlCondition);
// "WHERE Name = 'Leo' OR Id = 1"
}
static string GetSql<T>(IEnumerable<T> models, Expression<Func<T, bool>> condition)
{
// Code
var sqlString = "";
return "WHERE " + sqlString;
}
}
How to change GetSql method to sqlCondition
print WHERE Name = 'Leo' OR Id = 1
?