Entity Framework Plus Core Filter

32 Views Asked by At

I did not find example for my case.

My model:

public class Data
{
    public int ID {get; set;}
    public string? Name {get; set;}
    public virtual ICollection<Property>? Properties { get; set; }
}

public partial class Property
{
    public int? ID              { get; set; }
    public string? Name         { get; set; }
    public string? Value        { get; set; }
    public int?    ParentID     { get; set; }

    public virtual Data? Parent { get; set; }
}

My query:

var condition = "x.Properties != null && x.Properties.Any( y => y.Name == \"Date\" && y.Value == \"12/11/2022\" )";

var result = context.Data.Include( x => x.Properties ).WhereDynamic( x => condition );

And everything works fine,

but I need something more complex:

var condition = "x.Properties != null && x.Properties.Any( y => y.Name == \"Date\" && y.Value != null && y.Value.ToDateTime(103) < DateTime.Parse(\"12/11/2022\") )"; 

Where ToDateTime(103) is my Extension method to convert string to DateTime.

I got Exception when execute query:

Oops! No applicable member has been found for the expression. The error occurred for expression "." at position 3 near ".Where(x => x.Properties".

When I run above query using plain WHERE everything works

var result = context.Data.Include( x => x.Properties ).Where( x => x.Properties != null && x.Properties.Any( y => y.Name == \"Date\" && y.Value != null && y.Value.ToDateTime(103) < DateTime.Parse(\"12/11/2022\") )");
1

There are 1 best solutions below

1
Jakub Fojtik On

Per this github issue you must register your extension method before using it:

EvalManager.DefaultContext.RegisterType(typeof(Your.DateTimeExtensionsClass));