0

My LINQ query has a piece like

where dataContext.MyTable.Any(r => $"%{r.Name}%" == name)

which is causing an exception

"Method 'System.String Format(System.String, System.Object)' has no supported translation to SQL

Possible to do what I'm trying to do or will I have to resort to writing raw SQL?

2
  • Why raw SQL? Just assign $"%{name}%" to a variable outside of the query and use it inside. Commented Feb 1, 2017 at 20:37
  • @IvanStoev, whoops, I had something backwards. Watch my edit. Commented Feb 1, 2017 at 20:39

1 Answer 1

3

Instead of using string interpolation (which uses String.Format underneath), just use string concatenation:

where dataContext.MyTable.Any(r => ("%" + r.Name + "%") == r.Name)
Sign up to request clarification or add additional context in comments.

1 Comment

@DrainTheSwamp - That's why it always helps to know what's going on behind the curtain when using shortcuts. Feel free to upvote/accept.

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.