2

I'm trying to perform a LIKE clause in an entity query. The examples I've seen use dynamic linq to do this kind of thing, but my code...

var query = context.StudySet
    .Where("it.PatientName LIKE @patientName", new ObjectParameter("patientName", patientName);

...gives me a System.Linq.Dynamic.ParseException with

Additional information: Expression of type 'Boolean' expected

context is a DbContext from EF 6 code-first, patientName is a string

Please tell me what's wrong with my code?

Community
  • 1
  • 1
nopskazoid
  • 169
  • 2
  • 10
  • Wouldn't something like `var query = context.StudySet.Where((it) => it.PatientName.Contains(patientName))` work for you, without using Dynamic LINQ? – Mark May 09 '14 at 19:26
  • The value for patientName will come from a search box which will take wildcards. I guess I can split the string and build up a query combining StartsWith, Contains, EndsWith etc. I just saw LIKE as being a bit less code. – nopskazoid May 09 '14 at 19:32
  • That makes sense, splitting it doesn't sound like fun! Maybe [`SqlMethods.Like`](http://msdn.microsoft.com/en-us/library/bb355235.aspx) would work, like in [this answer](http://stackoverflow.com/a/3913727/2278086)? Just not sure it it works with EF. – Mark May 09 '14 at 19:46
  • No that's right, it doesn't work, it's Linq to SQL only unfortunately. – nopskazoid May 09 '14 at 19:57
  • 1
    Sorry - I guess I'm going down the path you have already travelled! How about using `SqlQuery` and raw SQL, something like `var query = context.StudySet.SqlQuery("select * from StudySet where PatientName LIKE @patientName", new SqlParameter("@patientName", patientName));`? – Mark May 09 '14 at 20:23
  • Yeah that'd work. Thanks Mark. – nopskazoid May 09 '14 at 20:52
  • Have you tried `var query = context.StudySet.Where(s => s.PatientName.Contains(userInput))` -- I realize this is not Dynamic Linq, but if you are hard coding your table name in a string, this would be the same. – Nate May 09 '14 at 22:39

3 Answers3

4

if you want use DynamicLINQ you need change your code like this

var query = context.StudySet.Where("it.PatientName.Contains(@0)", patientName);

because DynamicLinq can't parse LIKE operator

Grundy
  • 13,356
  • 3
  • 35
  • 55
  • 1
    LIKE should be [supported](http://msdn.microsoft.com/en-us/library/vstudio/bb399359(v=vs.100).aspx). I don't have to use Dynamic Linq, I want to do a LIKE. Contains != LIKE (see above). – nopskazoid May 11 '14 at 20:48
  • 1
    @nopskazoid eSQL not the same DynamicLINQ :-) possibly you need change tags for question – Grundy May 12 '14 at 06:46
2

I've realised my mistake.

I had assumed the method to pass the query was part of Dynamic Linq but actually it's just a variant of the standard Where method on ObjectQuery. If I get the ObjectContext from my (code first) DbContext it's all good.

ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext;
ObjectSet<Study> studySet = objectContext.CreateObjectSet<Study>();

var query = studySet.
    Where("it.PatientName LIKE @patientName", new ObjectParameter("patientName", patientName));
nopskazoid
  • 169
  • 2
  • 10
0

I don't know of a way to use like with a LINQ query, but you could use a raw SQL query:

var query = context.StudySet.SqlQuery(
    "select * from StudySet where PatientName LIKE @patientName",
    new SqlParameter("@patientName", patientName));
Mark
  • 8,140
  • 1
  • 14
  • 29