Dynamic Linq to add LIKE to where clause

dynamic-linq entity-framework linq

Question

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?

1
2
5/23/2017 10:30:09 AM

Accepted Answer

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));
2
5/12/2014 8:02:26 AM

Popular Answer

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



Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow