Search code examples
c#replacesql-server-2008-r2lowercasedynamic-linq

LOWER and REPLACE doesn't work on dynamic linq where statement


Here is my code ,

db.myDBContext.my_tables.Where("REPLACE(LOWER(name),\" \",\"\") == \"{0}\"", value);

it show the error

No applicable method 'LOWER' exists in type 'my_table'  

can't I use REPLACE and LOWER in dynamic linq clause ?


Solution

  • Dynamic Linq doesn't understand T-SQL. You will want to craft it this way:

    .Where(string.Format("(name).ToLower().Replace(\" \", \"\") == \"{0}\" ", value))
    

    There is an analog for ToLower and Replace in T-SQL and Linq knows how to translate them from c#. But if name is a static column name then @Jonny is on to something. You don't need Dynamic Linq here (unless this is just a contrived example of a bigger problem you are solving).