-
-
Notifications
You must be signed in to change notification settings - Fork 231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use Dynamic LINQ with custom types (i.e NodaTime) ? #477
Comments
Hi @StefH, since we are using NodaTime ubiquitously it means we can't use Dynamic LINQ without addressing this issue. If there is no built in mechanism that would help to solve this problem maybe you would be willing to accept contribution for one and maybe you have some suggestions on approach that would be prefered. |
Maybe a possibility could be to add support for a new DynamicLinq attribute for each property? class Entity
{
[DynamicLinqTypeConverter(typeof(LocalDateConverter)]
public LocalDate Date {get; set;}
[DynamicLinqTypeConverter(typeof(LocalTimeConverter)]
public LocalTime? Time {get;set:}
} However once this is added, I don't know yet how this can be used/accessed within the logic... |
@myshon Can you provide a full working example console app? |
@StefH annotating every property in a class which uses type from NodaTime with |
@KrzysztofBranicki I see your point. Can you please provide an example project which uses postgress database and uses Npgsql.NodaTime ? |
@StefH It didn't use Postgres nor Npgsql.NodaTime, but I believe it's not needed to point the case. Key line is here: var expr = DynamicExpressionParser.ParseLambda<Entity, bool>(config, false,
"BirthDate == @0", "1987-10-12"); without var expr = DynamicExpressionParser.ParseLambda<Entity, bool>(config, false,
"BirthDate == @0", new LocalDate(1987,10,12)); but it is not satisfying because I don't know the type of parameter during parsing. The solution we expect is a possibility to register converter for parsing only (not globally) like @KrzysztofBranicki mentioned DynamicExpressionParser.RegisterTypeConverterFor<LocalDate>(typeof(LocalDateConverter); or alternatively, provide a list/dictionary of converters in var config = new ParsingConfig()
{
TypeConverters = new ()
{
{typeof(LocalDate), new LocalDateConverter()},
{typeof(LocalTime), new LocalTimeConverter()}
};
} What do you think about it? |
BTW : I noticed that when you add this attribute to a property, it also seems to work fine: private class Entity
{
public Guid Id { get; set; }
public string FirstName { get; set; }
[TypeConverter(typeof(LocalDateConverter))] // <------
public LocalDate BirthDate { get; set; }
} (However in your case this would not be an option? Because you don't want to annotate all classes....) |
Hello @myshon, I did add your test to the test project: However the test does not fail? |
Hi @StefH, Will NodaTime 3 solves this issue? Partially... First of all, we would like to have a solution which works not only for node types. I already prepared PR for this feature in Dynamic Linq but I don't have permission to push it into repo. You can review my changes. |
@myshon and @KrzysztofBranicki You can add extra converters via the parserconfig: TypeConverters Example: var parsingConfig = new ParsingConfig
{
TypeConverters = new Dictionary<Type, TypeConverter>
{
{ typeof(LocalDate), new LocalDateConverter() }
}
}; |
Thanks @StefH However, while testing, I found another intersting issue. I suppose it's not related with converters but null comparison. Having entity private class Entity
{
public DateTime? EmployedAt { get; set; }
} we can create the following expression successfully ✔️ DynamicExpressionParser.ParseLambda<Entity, bool>(config, false, "EmployedAt != null"); However, when I change
Both types |
@myshon and @KrzysztofBranicki |
|
Hello @myshon, did you also try to run this with a real Postgres database? |
Hi @StefH |
Official version will be released within some time, until then, you can use the preview version. |
Hi @StefH , i'm using .NET 6 with Dynamic Linq and Microsoft.EntityFrameworkCore.DynamicLinq 6.2.19 but i can't configure and override custom TypeConverters.
My custom converter class is:
I execute my query passing the config but none of the above methods is calling and i can't override converters: |
Hi,
Let's use an entity with external types (in my case
NodaTime
). I use Postgres with Nodatime converters fromNpgsql.NodaTime package
(which provides not only mapping between Postgres types and Noda types but also allows query by these)Here is an entity:
I would like to query
Date
andTime
using dynamic Linq expression.By default, it's not possible, the code above throws
NotSupportedException
. It can be easily solved using type converters.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:
The text was updated successfully, but these errors were encountered: