Skip to content

How to use Dynamic LINQ with custom types (i.e NodaTime) ?  #477

Closed
@myshon

Description

Hi,
Let's use an entity with external types (in my case NodaTime). I use Postgres with Nodatime converters from Npgsql.NodaTime package (which provides not only mapping between Postgres types and Noda types but also allows query by these)

Here is an entity:

class Entity
{
   public LocalDate Date {get; set;}
   public LocalTime? Time {get;set:}
}

I would like to query Date and Time using dynamic Linq expression.

var list = entities
      .Where("Date >= @0", date)
      .ToList();

By default, it's not possible, the code above throws NotSupportedException. It can be easily solved using type converters.

TypeDescriptor.AddAttributes(typeof(LocalDate), new TypeConverterAttribute(typeof(LocalDateConverter)));
TypeDescriptor.AddAttributes(typeof(LocalTime), new TypeConverterAttribute(typeof(LocalTimeConverter)));

Then it works as expected... Almost.

The problem is that TypeDescriptor.AddAttributes adds converters globally which affects many places (like Newtonsoft JSON serializer) in a stable, working legacy system. It's very risky, I would like to avoid this way.

Is there any different approach how to register converter for custom type?

Here. list of package details I use:

<PackageReference Include="Microsoft.EntityFrameworkCore.DynamicLinq" Version="3.2.6" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" />
<PackageReference Include="Npgsql.NodaTime" Version="4.1.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.4" />

Metadata

Assignees

Labels

Type

No type

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions