2

I have installed System.Linq.Dynamic dll and then tried to add string as the parameter of WHERE clause in Linq. But I am still getting error that the string parameter is supported by WHERE clause.

Code:

_dbContext.TmRecords.Where("city=london");

Error:

Severity Code Description Project File Line Error CS1503 Argument 2: cannot convert from 'string' to 'System.Linq.Expressions.Expression>' Extranet.Domain

Here the city parameter dynamically changes to some other parameter. So, I need to use dynamic queries in linq.

6
  • You need to create a predicate, such as: _dbContext.TmRecords.Where(record => record.city = "london"); Commented Oct 20, 2016 at 9:53
  • Possible duplicate of Dynamic WHERE clause in LINQ Commented Oct 20, 2016 at 9:54
  • 1
    @Mark it's about dynamic Linq, not regular Linq
    – Kilazur
    Commented Oct 20, 2016 at 9:54
  • 2
    My first guess in such cases would be that you forgot the correct using
    – Kilazur
    Commented Oct 20, 2016 at 9:55
  • 2
    Do you have using System.Linq.Dynamic; at the top of your file? Without that, the only method that the compiler can find is the one expecting an expression and not a string.
    – DavidG
    Commented Oct 20, 2016 at 9:57

1 Answer 1

7

You should add using System.Linq.Dynamic; to your file.

Also rewrite the query like this:

_dbContext.TmRecords.Where("city = @0", "london");
4
  • using the @0 is necessary or just good practice? Thanks, this helped me aswell :)
    – Roy123
    Commented Oct 20, 2016 at 10:03
  • The same thing how can i use it in lamda expression?
    – Dinesh
    Commented Oct 20, 2016 at 10:22
  • 4
    It is not necessary. You can use "City == \"london\"" instead but using parameterized one is more readable IMHO. @Roy_Dorsthorst Commented Oct 20, 2016 at 10:31
  • If you have another question then ask it in new question. @dineshsns Commented Oct 20, 2016 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.