0

I have a class with a property that contains serialized data in json format:

public class MyObject
{
    public string SerializedData 
    {
        get
        {
            ...
        }
        set
        {
            ...
        }
    }
}

The object is a property on a entity framework database object:

public class MyRecord
{        
    public int Id { get; set; }

    public MyObject Item { get; set; }
}

I'm now trying to write a LINQ statement to check if the serialized string contains my search text. I'm building the LINQ statement dynamically:

var predicate = PredicateBuilder.New<TEntity>();
var param = Expression.Parameter(typeof(TEntity), typeof(TEntity).Name);
Expression property = Expression.Property(param, type.Name);

Expression contains = Expression.Call(
            property, "Contains",
            null,
            new[] { Expression.Constant("test") });

        predicate.Or(Expression.Lambda<Func<TEntity, bool>>(contains, param));

My object doesn't contain the "Contains" method so I add one:

public class MyObject
{
    ...
    public bool Contains(string value)
    {
        return true;
    }
}

When my query runs I get the following error:

LINQ to Entities does not recognize the method 'Boolean Contains(System.String)' method, and this method cannot be translated into a store expression.

I not sure if I'm even approaching this in the correct way?

Can anyone help please?

Thanks

Sun
  • 4,458
  • 14
  • 66
  • 108
  • Custom methods don't work with LINQ to Entities. You need to create an equivalent of `x => x.Item.SerializedData.Contains(...)` – Ivan Stoev Dec 27 '18 at 15:38
  • Alternatively, you can separate the EF and local evaluation by putting `AsEnumerable()` in between. – NetMage Dec 27 '18 at 18:51

0 Answers0