I wanted to build code to iterate properties of any object and build dynamic where clause in LINQ based on fields that have a valid value. I have it working.
However, in the code below you will see commented lines that use string interpolation and they simply do not work. The comment explains, but examining the generated SQL it is because the WHERE clause is completely wrong. This only happens for decimal fields and the simply string is the one that works.
It is not the end of the world but I am going to lose sleep as to why LINQ generates this . . . any ideas or help? Thanks much!
// TODO: why does the string interpolation not work?
// it outputs BudgetApproved.Equals(115500.00) which for some reason when query is
// constructed becomes FROM [Tactic] AS [t] WHERE CAST(0 AS bit) = CAST(1 AS bit)
string whereClause = fieldName + " = " + fieldValue;
//string whereClause = string.Format("{0}.Equals({1})", fieldName, fieldValue);
//string whereClause = $"{fieldName}.Equals({fieldValue})";
query = query.Where(whereClause);
UPDATED
Looks like @NetMage suggestion works but it's odd because in immediate window two outputs seem to be the same.
?fieldName + ".Equals(" + fieldValue + ")";
"ExpenseCategoryItemId.Equals(1)"
?string.Format("{0}.Equals({1})", fieldName, fieldValue);
"ExpenseCategoryItemId.Equals(1)"